Newbie: formatting numbers

  • Thread starter Thread starter steve
  • Start date Start date
S

steve

Hi,

This is probably a stupid question but I couldn't find a *simple*
satisfactory answer on MSDN:

How , *irrespective of the user's regional settings*, can i format numbers
in a ***simple*** way?

All i want is comma separation for thousands and rounding to two decimal
palces.

i.e : 123456.4566 will be 123,456.46

TIA
-steve
 
Thanx Jay,

However as I said i was hoping for something simple and not dependent on
regional settings.
Do i have to read all this documentation? and also in their examples it
clearly states the results *depending* on the regional settings.

-steve
 
steve,

On example

Have you an idea what value 123456.4566 in characters is this in continental
Europe

To give you a concrete exact answer on your question.

\\\
Threading.Thread.CurrentThread.CurrentCulture = _
New Globalization.CultureInfo("en-US")
MessageBox.Show(Format(123456.4566, "N"))
Threading.Thread.CurrentThread.CurrentCulture = _
New Globalization.CultureInfo.InstalledUICulture
///

However in this way used is it in my opinion a stupid use of the
globalization settings and can give you a lot of trouble, when the program
is used outside the Us culture and therefore useless.

Cor
 
Cor Ligthert said:
\\\
Threading.Thread.CurrentThread.CurrentCulture = _
New Globalization.CultureInfo("en-US")
MessageBox.Show(Format(123456.4566, "N"))
Threading.Thread.CurrentThread.CurrentCulture = _
New Globalization.CultureInfo.InstalledUICulture
///

However in this way used is it in my opinion a stupid use of the
globalization settings and can give you a lot of trouble, when the program
is used outside the Us culture and therefore useless.

ACK.

\\\
Imports System.Globalization
..
..
..
MsgBox( _
123456.4566.ToString( _
"N", _
CultureInfo.InvariantCulture _
) _
)
///
 
Thanx Jay,

However as I said i was hoping for something simple and not dependent on
regional settings.
Do i have to read all this documentation? and also in their examples it
clearly states the results *depending* on the regional settings.

-steve
Dim x As Double
x = 12232.25
Console.WriteLine(x.ToString("N", CultureInfo.InvariantCulture))
 
Steve,
How does that saying go: If you give a man a fish he eats for a day, if you
teach a man to fish he eats for live.

If you had read the entire thing you will notice that the Standard Numeric
Format Strings are normally dependant on regional settings, as Herfried &
Austin showed you can pass a CultureInfo.InvariantCulture, which is region
independent.

If you had read at least the third link on custom formats, you would notice
that custom formats are not region specific, expect for the place holders,
again if you pass an InvariantCulture, then they become region independent
again. The samples include one or more custom formats that should do what
you want.

So yes, if you really want to know how formatting works in .NET you should
read the entire section.

Thanks for understanding

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top