converting values to 2 decimal places?
-
Friday, May 11, 2007 9:37 AM
Hi,
I have some values which are stored as money type in sql server. The problem is when i got the values i have this 4 digits after a floating point which is annoying.
I need to show the users only 2 digits after the floating point and i'm struggling in this?
What do i need to do to show the users like this, should i change the database type if it's not possible or restrict the user when the user put in those values on the first place?
Please helpThanks
All Replies
-
Friday, May 11, 2007 12:07 PMModerator
I don't think you want to convert it to 2 decimal places (the whole point of the money type is so people don't actually loose money when rounding - think Superman), but display it to 2 decimal places:
There are several ways. The one I use most frequently is the String.Format method.
Code SnippetDim value As Single = 34.68921 ' Just a number
Debug.WriteLine(String.Format("${0:#,##0.00}", value))
Debug.WriteLine(String.Format("{0:c}", value))
The first Debug statement explicitly formats the value. The second formats it to the system regional currency.
if you wish to round the penny down, then you could do something like:
Code SnippetDebug.WriteLine(String.Format("{0:c}", Math.Floor(value * 100) / 100))
The Math namespace contains a lot of useful functions.
-
Friday, May 11, 2007 12:17 PMModerator
I should have posted a link to composite formatting, as it can be a bit confusing:
Composite Formatting
http://msdn2.microsoft.com/en-us/library/txafckwd.aspx
(Composite formatting is the use of 'curly braces' in the above example).
Which is linked to in the String.Format Method:
String.Format Method (string, object)
http://msdn2.microsoft.com/en-us/library/fht0f5be.aspx

