# 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.
Monday, June 1, 2009 10:20 PM

• Marked as answer by Tuesday, June 2, 2009 8:17 PM
Tuesday, June 2, 2009 8:17 PM

### 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

Monday, June 1, 2009 10:37 PM
• Try

x = 100 * 8.75 /100 * 2
GraphicsWindow.DrawText(0,0,x)

I used 2 numbers behind decimal point and the result also came out as 2 numbers behind decimal point.

Monday, June 1, 2009 10:47 PM
• 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)

Tuesday, June 2, 2009 1:32 AM
• Thanks litdev,
This works but if dollars = 100, I still would not have the 100.00 format. I was looking to get currency format even if the variable was a whole number.
Tuesday, June 2, 2009 3:43 PM
• Thanks Khumara,
If x is a whole number like 100, It would not return back to 2 decimal places. I was looking for something like a currency format, sorry I did not make that clear on my post.
Tuesday, June 2, 2009 3:45 PM
• Thanks rushworks,
This is a nice way to do it but if the numbers divided equal a whole number ( 400 / 100), it does not return decimals. I was looking for something like a currency format, sorry I did not make that clear on my post.
Tuesday, June 2, 2009 3:47 PM
• 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)

Tuesday, June 2, 2009 4:43 PM
• 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.
Tuesday, June 2, 2009 4:55 PM
• 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))
Tuesday, June 2, 2009 8:12 PM
• Marked as answer by Tuesday, June 2, 2009 8:17 PM
Tuesday, June 2, 2009 8:17 PM
• The code:

--------------------------------------------

'How to format a number for currency.
TextWindow.Writeline("Type a number to convert to currency")
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 Monday, February 6, 2017 1:10 AM
Monday, February 6, 2017 1:07 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 Monday, February 6, 2017 1:26 PM
• Unmarked as answer by Monday, February 6, 2017 1:30 PM
• Proposed as answer by Monday, February 6, 2017 1:30 PM
Monday, February 6, 2017 10:18 AM
• or to go further: add a dollar sign like this:

purchases=74.678954

purchases=math.Round(purchases*100)/100
purchases="\$ "+purchases
GraphicsWindow.DrawText(50,50,purchases)

Monday, February 6, 2017 1:27 PM

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 Wednesday, February 8, 2017 1:34 PM
Wednesday, February 8, 2017 2:49 AM
• 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)
EndFor
textwindow.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)
EndFor
textwindow.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)

******************************************************************************************

Wednesday, February 8, 2017 1:34 PM