Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / printing only 2 decimal places on a float

Author
Message
PeterLewis
20
Years of Service
User Offline
Joined: 27th Sep 2003
Location: South Africa
Posted: 2nd Oct 2003 13:18
Hi

I have a variable as a float. How do I restrict it to only display only 2 decimal places. At the moment it changes all the time and interfers with other text on the screen.

Thank you in advance
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 2nd Oct 2003 14:20
Don't know if there's an official way, but you could always convert it to a string and do some string manipulation to display what you want.

BatVink (formerly StevieVee)
http://facepaint.me.uk/catalog/default.php
CattleRustler
Retired Moderator
21
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 2nd Oct 2003 19:40
write a function that takes your number as an argument, changes to string, truncates any extra decimal places, returns a string to the call, display your string - This may slow you down however.

it would be nice if dbp had a "Format" Function like vb

-RUST-
Xander
21
Years of Service
User Offline
Joined: 3rd Mar 2003
Location: In college...yeah!
Posted: 2nd Oct 2003 21:24
Really easy...

newnum=int(num*100)/100

Should work...

Xander - Bolt Software

It doesn't matter if it is confusing, as long as it works
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 2nd Oct 2003 22:51
Try this code to see if it would be useful to you

I've only lightly tested it but it should be Ok for most things.
PeterLewis
20
Years of Service
User Offline
Joined: 27th Sep 2003
Location: South Africa
Posted: 3rd Oct 2003 02:33
Thank you to IanM for working out this function. Only problem it has is that it does not round off , it cuts off.

The rest of the function works well

Any ideas how to solve the rounding problem ?
CattleRustler
Retired Moderator
21
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 3rd Oct 2003 19:24
modify ian's function so that before it returns the 2-digit decimal, it looks at the numeric value of it, and changes it up or down based on the number it is currently

-RUST-

Login to post a reply

Server time is: 2024-09-21 03:34:28
Your offset time is: 2024-09-21 03:34:28