Rounding numbers
Hi, I want a function to pass in a number and have it returned as a string formatted for accounting purposes so
1 should return £1.00
1.1 should return £1.10
1.25 should return £1.25
so 2 decimal places EVEN IF the source number is whole i.e 1000 should be £1000.00
I have this but it doesn't seem to work when the source number is whole, 1000 returns £1000
Public Shared Function Cn(ByVal amt As Decimal) As String
Dim a As Decimal = System.Math.Round(amt, 2)
Cn = String.Format("£{0:###,###.##}", a)
End Function
Can anyone help?
Replace the last two hashes with zeros.
For example:
String.Format("£{0:###,###.00}")
However, the problem with the above code is that it isn't international aware, that is, the code above will only be correct for the UK.
It is better to use the standard formats, for example, for currency:
String.Format("{0:C}")
This will correctly display the correctly format for the current culture of the thread, therefore if you are in Australia, 1.25 would be displayed as $1.25.
The following page shows the standard formats available for .NET:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconcompositeformatting.asp