Hi Anna!
Anna said:
I'm always struggling with formatting numbers, despite all the online
examples.
I need to format 12470 to 124.70.
As all answeres proved so far: If a users reads both values he would normaly
expect that you mean 2 different values.
One easy possibility could be: You simply made a small mistaka and the 2nd
number should have been 12.470. In some other posting you wrote that the 470
is of type long or so ...
Then you need to check the CultureInfo and the ToString implementations in
the msdn library. And you should look for "globalosation". I am quite sure
you will find something.
(Different cultures expect different formats. Some want 1.234,56 others want
1,234.56 or so ... date/time is much more strange ....)
If it is simply a formatting issue for you: Check IFormatProvider,
IFormattable and IFormatter in MSDN. Then you see, which interfaces you
could use to give a class your own formatters.
BUT: Think about the meaning of the value. Maybe one example could be: You
store the number of Cents (The money here is counted in Euro where 1 Euro is
100 Cent) in a long value. Of course the User expects the value in Euro. I
would prefer something like this over writing some formatters:
int moneyInCents = 12470; // We always have to count the Cents ...
float moneyInEuro = moneyInCent / 100; // I want the money in Euro to
display it to the user
// Some normal output like moneyInEuro.ToString("C");
Reasons are:
- It is much easier to read.
- people get, what they expect. (And they normaly do not expect 12470 =
123.70. It is more or less confusing them.)
- It is much easier to implement (Look at the interfaces and you know what I
mean
![Smile :) :)](/styles/default/custom/smilies/smile.gif)
Remember that you should take care of the current culture and print
the value as the user expects!)
Just my 2 cent - I hope they was of any help.
With kind regards,
Konrad