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.

DarkBASIC Professional Discussion / How to print only 2 decimals after the point in float

Author
Message
mhack
23
Years of Service
User Offline
Joined: 24th Apr 2003
Location:
Posted: 15th Oct 2003 17:37
I'm ptining or using text to display my float numbers
And I need to display 2.00 instead of 2.000000009996?

Thank you,
Arseny
Codger
23
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 15th Oct 2003 19:09
it should be a# = int(a#*100)/100 but it does not work in DBP

System
PIII 650 MZ H.P. Pavillion
394 Mem GeForce 4 400MX
Van B
Moderator
23
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 15th Oct 2003 19:18
Hmmmm, try this instead:

a# = int(a#*100.00)/100.00

Might work, DB is fairly psychotic sometimes . The .00 I added is to bring it to a real number, I reckon the 100 on it's own was forcing an integer.


Van-B

Van B
Moderator
23
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 15th Oct 2003 19:20
Ohh, I made a function ages ago that gave a VB style format command, but with numerics only (e.g. a$=format(a#,"0.00"), I'll post it later tonight if I remember where it is.


Van-B

Richard Davey
Retired Moderator
24
Years of Service
User Offline
Joined: 30th Apr 2002
Location: On the Jupiter Probe
Posted: 15th Oct 2003 19:21
This does however:

a# = 652.009093490394039
print left$(str$(a#),len(str$(int(a#)))+3)

Change the +3 to however many decimal points you need to display. This version will work regardless of how many leading figures there are. If you know it will always be a value less than 10 then you can use the simpler:

print left$(str$(a#), 4)

Cheers,

Rich

"It's easy to be mean when death equals a high score screen."
"You can take your Quake and go away, I'd rather play Bubble Bobble any day."
indi
23
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 15th Oct 2003 19:21
this is one way if its intended for a direct output to the screen but hardly effective in a maths problem




Van B
Moderator
23
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 15th Oct 2003 19:28
I suppose it depends on how important rounding off is, a calculater would call 3.146 as 3.15 whereas text methods like those would call it as 3.14.

There's also the other problem of .5 - 3.50 would be returned as 3.5 in a maths method, but 3.5<space> in a text method.

Great, now I have a headache!


Van-B

Codger
23
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 15th Oct 2003 19:35
Here is a function that makes it tidy

a# = 652.109093490394039
FormatPrint(a#,2,20,20)

wait key

Function FormatPrint(a#,n,x,y)
Set Cursor x,y
Print left$(str$(a#),len(str$(int(a#)))+n+1)
EndFunction

System
PIII 650 MZ H.P. Pavillion
394 Mem GeForce 4 400MX
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 15th Oct 2003 20:05
I posted this one for someone a week or so ago that pads the output if necessary

mhack
23
Years of Service
User Offline
Joined: 24th Apr 2003
Location:
Posted: 15th Oct 2003 21:27
Thank you guys for so many answers!
WOW!

I think I will go with simple one just for printing
Thank you,
Arseny
Ian T
23
Years of Service
User Offline
Joined: 12th Sep 2002
Location: Around
Posted: 16th Oct 2003 01:39
IanM-- thanks, I've been looking for something accurate like that for a while. Left$ worked until rounding off numbers became a big deal with the kind of detail I was working with. *shrugs*

--Mouse: Famous (Avatarless) Fighting Furball

A very nice %it, indeed.

Login to post a reply

Server time is: 2026-07-26 09:31:20
Your offset time is: 2026-07-26 09:31:20