Answered by:
Format numbers
Question

Me again,
Is there a good way to format numbers, say as dollars; $100.00
In my program I multiply say an amount 100 * .0875 * 2 and get 17.50000
I would like this to read 17.50 when I use graghicwindow.drawtext
Once again, I really appreciate everyone's help on this forum, the support is unbelievable.
Answers
All replies

Try the following to either format the number by converting to cents then round the cents to an integer then divide by 100, or convert to text and use the decimal point to format to 2 significant figures.
dollars = 100 * .0875 * 2
TextWindow.WriteLine(dollars) 'Unformatted
TextWindow.WriteLine(Math.Floor(dollars*100)/100) ' Formatted to round to nearest 2 decimal places without trailing zeros
TextWindow.WriteLine(Text.GetSubText(dollars,1,Text.GetIndexOf(dollars,".")+2)) ' Formatted to include first 2 decimal places after decimal point


Here's a method that allows you to set how many decimal places you want.
Number = 12345678 / 9876
Decimals = 8
Number = Math.Floor(Number * Math.Power(10, Decimals)) / Math.Power(10, Decimals)
TextWindow.WriteLine(Number)
Decimals = 4
Number = Math.Floor(Number * Math.Power(10, Decimals)) / Math.Power(10, Decimals)
TextWindow.WriteLine(Number)
Decimals = 2
Number = Math.Floor(Number * Math.Power(10, Decimals)) / Math.Power(10, Decimals)
TextWindow.WriteLine(Number) 



This code will check for whole numbers and add '.00' to the end if needed
Number = 100
Decimals = 2
Number = Math.Floor(Number * Math.Power(10, Decimals)) / Math.Power(10, Decimals)
If (Math.Remainder(Number, 1) = 0) Then
Number = Text.Append(Number, ".")
For I = 1 To Decimals
Number = Text.Append(Number, "0")
EndFor
EndIf
TextWindow.WriteLine("$" + Number) 
Wow, thanks Rushworks, you saved me about 3 days of trying to figure this out. It pays to know what all the classes can do, I only wish there was some sort of user's guide. I know of the samll basic API reference web site (it is very useful), but something in writting showing examples of all the commands would be a time saver.

Yet another  a bit of a peculiarity of Small Basic, but following Khumara's method seems to work for all integers and reals in variable 'dollars'.
TextWindow.WriteLine("$" + 0.01*Math.Floor(dollars*100)) Edited by litdevModerator Tuesday, June 2, 2009 8:14 PM simplified


The code:

'How to format a number for currency.
TextWindow.Writeline("Type a number to convert to currency")
number = TextWindow.ReadNumber()
currency = (Math.Round(number * Math.Power(10, 2)) * Math.Power(10, 2)) 'this is the important line.
TextWindow.Writeline("$" + currency)
Example output:
Type a number to convert to currency
45.555
$45.56
Press any key to continue... Edited by MJRofick Monday, February 6, 2017 1:10 AM

Why is this:
currency = (Math.Round(number * Math.Power(10, 2)) * Math.Power(10, 2))
better than:
currency = Math.Round(number * 100) /100
??
Jan [ WhTurner ] The Netherlands
 Marked as answer by YLedEditor Monday, February 6, 2017 1:26 PM
 Unmarked as answer by YLedEditor Monday, February 6, 2017 1:30 PM
 Proposed as answer by YLedEditor Monday, February 6, 2017 1:30 PM


My Answer:
currency = (Math.Round(number * Math.Power(10, 2)) * Math.Power(10, 2))
is better than:
currency = Math.Round(number * 100) /100
because:
Math.Power(10, 2) forces proper scientific decimal point notation.
So, the number 0 * Math.Power(10, 2) will display properly as 0.00, instead of just 0.
 Proposed as answer by YLedEditor Wednesday, February 8, 2017 1:34 PM

Hi MJRofick
your absolutely right !! it gives 0,00 instead of a 0 alone.!
Thanks for sharing this amazing discovery!!!Now I wrote a small program that compares the two methods and with different numbers.
Indeed, where there is a zero, the format 0.00 is to consider when we have case of monetary values.*******************************************************************************************
number[1]=125.458765*0
number[2]=2789025.458765*0
number[3]=0
number[4]=34567.890444444444444
number[5]=125.458765*0
number[6]=(125.458765*0)+167.7890/5.4
number[7]=Math.Pi
number[8]=Math.Power(10,24)*5.98567485
textwindow.writeline("")
textwindow.writeline("_______________________________________________________________________________________")
TextWindow.WriteLine(" method 1 = Math.Round(number[t] * Math.Power(10, 2)) * Math.Power(10, 2)")
For t=1 To 8
currency = (Math.Round(number[t] * Math.Power(10, 2)) * Math.Power(10, 2))
TextWindow.WriteLine(currency)
EndFortextwindow.writeline("")
textwindow.writeline("")
TextWindow.WriteLine(" method 2 = Math.Round(number[t] * 100) /100")
TextWindow.WriteLine("_______________________________________________________________________________________")
For t=1 To 8
currency = (Math.Round(number[t] * 100) /100)
TextWindow.WriteLine(currency)
EndFortextwindow.writeline("")
textwindow.writeline("")
TextWindow.WriteLine(" exponent number")
TextWindow.WriteLine("_______________________________________________________________________________________")special=Math.Power(10,5)*5.98567487
TextWindow.WriteLine(special)
value = (Math.Round(special * Math.Power(10, 2)) * Math.Power(10, 2))
TextWindow.WriteLine(value)textwindow.writeline("")value2 = (Math.Round(special * 100) /100)
TextWindow.WriteLine(value2)******************************************************************************************