format a "numeric" string

  • Thread starter Thread starter rocio
  • Start date Start date
rocio said:
How can I format a string like this: 12924.999999999999
to print 12,925 ?


dim s as string = "12924.999999999999"
dim d as decimal

d = decimal.parse(s)
s = d.tostring("#,##0")
 
Convert your string to a double, and then convert it back
to a string.
I use the following code to convert doubles to string:

Dim value as double
Dim result As String
Dim nfi As NumberFormatInfo
nfi = New CultureInfo("en-US", False).NumberFormat
nfi.NumberDecimalSeparator = "." 'set the decimal sep.
nfi.NumberGroupSeparator = "," 'set de grup sep.
nfi.NumberDecimalDigits = "0" 'set the number of decim.
result = value.ToString("N", nfi)

Try it out!
 
or ( having read this again )

MsgBox(Math.Round( Cdbl("12924.999999999998")))

Regards - OHM
 
yes, it works.

Tx!


BeGomes said:
Convert your string to a double, and then convert it back
to a string.
I use the following code to convert doubles to string:

Dim value as double
Dim result As String
Dim nfi As NumberFormatInfo
nfi = New CultureInfo("en-US", False).NumberFormat
nfi.NumberDecimalSeparator = "." 'set the decimal sep.
nfi.NumberGroupSeparator = "," 'set de grup sep.
nfi.NumberDecimalDigits = "0" 'set the number of decim.
result = value.ToString("N", nfi)

Try it out!
 
Rocio,
In addition to the other comments you can also use Double.ToString().

Both ToString & String.Format support different format strings.

Note if you come from a VB6 background Format is still supported, however I
tend to use the .NET equivalents, as the .NET usage applies to more places
in the Framework! (String.Format, StringBuilder.AppendFormat,
Object.ToString, Console.Write, StreamWriter.Write...)

For details on formatting in .NET see:

http://msdn.microsoft.com/library/d...y/en-us/cpguide/html/cpconformattingtypes.asp

For numerics see:
http://msdn.microsoft.com/library/d...us/cpguide/html/cpconnumericformatstrings.asp

For VB.NET runtime support see:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vafctFormat.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vafctFormatNumber.asp

Hope this helps
Jay
 
Back
Top