Converting a Float to String - decimal places

BlitzMax Forums/BlitzMax Beginners Area/Converting a Float to String - decimal places

Fry Crayola(Posted 2006) [#1]
When you convert a Float to a String (say, for example, to put the value in a TextField) it gives you the result to a certain number of decimal places depending on the size of the value.

Is there any easy way to force 2 decimal places, or is the only way to do it to run through the string until the decimal point is found, then cut it off two characters later?

Cheers.


ImaginaryHuman(Posted 2006) [#2]
You get different amounts of decimal places depending on how big the main part of the number is (the integer part). You can try to force two decimal places by multiplying your float by 100, turning it into an Integer to crop off the rest of the decimal places, and then displaying a decimal point right before you print the last two digits.


Dreamora(Posted 2006) [#3]
left(float_nr,instr(float_nr,".")+2)

As BM does not cut following "0" in floats as old Blitz did, this simple function works.


FlameDuck(Posted 2006) [#4]
is the only way to do it to run through the string until the decimal point is found, then cut it off two characters later?
Unless you know the order of magnitude of the number, then yes.


Fry Crayola(Posted 2006) [#5]
Oh, how I miss the old formatting techniques.

Only because I'm extremely lazy. I'll just steal Dreamora's snippet, which is better than mine because I tend to forget about the instr() function.

Cheers guys.