Float reduction

BlitzMax Forums/BlitzMax Beginners Area/Float reduction

dalaware(Posted 2008) [#1]
Hi,
Float reduction
How can I reduce the amount of places after the decimal point?
(Drawtext + Float) 1.50000000 I only need two places for my float.
Thanks.


TomToad(Posted 2008) [#2]
I believe there is code in the archives that'll allow you to print to an arbitrary decimal place.

Here's one: http://www.blitzbasic.com/codearcs/codearcs.php?code=1790


ImaginaryHuman(Posted 2008) [#3]
You can't. TomToad is suggesting that you convert your float to a string and then chop off the parts you don't want, which is okay, but as soon as you want to actually use that number to do something you'd have to convert it back to a float again.

Floats and doubles are NOT accurate enough to represent all numbers *exactly*, it is an approximation of a real number. Even numbers that you think would be easy to represent with less decimal places cannot be represented simply because that number may not fall on a numerical boundary supported by the number format.

You can only get exact numbers of decimal places by using either fractions or fixed-point numbers. To use fixed point numbers to 2 decimal places, just use Integers and multiple or divide them by 100 to represent the precision you need. e.g.

'I want 1.50,2.49

Local MyInt:Int=150
Local MyInt2:Int=249
MyInt2:+001           'add 0.01 to it
Plot MyInt/100.0,MyInt2/100.0

I guess it depends what you want to use it for.


dalaware(Posted 2008) [#4]
Hi

ImaginaryHuman "I guess it depends what you want to use it for."

I need to represent money in two decimal places


TomToad(Posted 2008) [#5]
For money that doesn't involve fractions of cents, you might best use fixed point. Basically, fixed point is an integer with an implied decimal point. So the value 1234 would actually be 12.34. To add or subtract fixed point numbers, it is just like adding or subtracting integers, just use + and -. To multiply, you need to divide the result by 100 (add 50 first if rounding is important). To divide, if you don't need rounding, you can just multiply the dividend by 100, then divide. If you do need rounding, then multiply the dividend by 1000, add 5, then divide by 10
'To add and subtract, just use + and - normally
Function FP_Multiply:Int(Value1:Int,Value2:Int,Round:int = True)
   Local Result:Int = Value1 * Value2 'Multiply the values together
   If Round Then Result :+ 50 'If rounding the result to nearest penny, just add 50
   Result :/ 100 'Divide the result by 100
   Return Result 'return the result
End Function

Function FP_Divide:Int(Dividend:Int, Divisor:Int,Round:Int = True)
   Local Result:Int
   If Round
      Dividend :* 1000 'Multiply by 1000
   Else
      Dividend :* 100 'Multilpy the Dividend by 100 for better precision
   End If
   Result = Dividend/Divisor 'Divide
   If Round
      Result = (Result + 5) / 10 'If rounding, Add 5 and devide by 10
   End If

   Return Result 'Return the result
End Function

To print, just print all but the right two numbers, decimal point, then the right two numbers
Function PF_ToString:String(Number:Int)
   Local Temp:String = String(Number) 'Convert the whole number to a string
   Local Result:String = Temp[..Temp.Length - 2] 'Extract all but the right two numbers
   Result :+ "." 'Add a decimal point
   Result :+Temp[Temp.Length-2..] 'Add the rest
   Return Result
End Function 



xlsior(Posted 2008) [#6]
I need to represent money in two decimal places


For that it seems to be the easiest to store the amount in an integer, counting by penny instead of dollars. When printing it, convert it to a string and insert the period between the 2nd and 3rd character from the right.