Format number

  • Thread starter Thread starter Anna
  • Start date Start date
A

Anna

I'm always struggling with formatting numbers, despite all the online
examples.
I need to format 12470 to 124.70.

Thanks in advance,

Anna
 
Anna said:
I'm always struggling with formatting numbers, despite all the online
examples.
I need to format 12470 to 124.70.

You can't, they are different numbers. What you can do, e.g., is convert an
integer 12470 to a floating-point 12470.0, then divide it by 100.0, and display
it as foo.ToString("#.00").

HTH,
-rick-
 
You can't, they are different numbers. What you can do, e.g., is convert
an integer 12470 to a floating-point 12470.0, then divide it by 100.0, and
display it as foo.ToString("#.00").

True (BTW in my case 2470 is a long value).
But i need to convert it to a string value. So i need some string
formatting.

Anna
 
Anna said:
True (BTW in my case 2470 is a long value).
But i need to convert it to a string value. So i need some string
formatting.


I must be misunderstanding your problem. The result of ToString() IS a string
value.

long lnum = 12470;
float fnum = (float)(lnum / 100.0);
string snum = fnum.ToString("#.00");

If this is not at least trending in the direction of what you are after, then I
am lost...

Regards,

-rick-
 
Anna said:
I'm always struggling with formatting numbers, despite all the online
examples.
I need to format 12470 to 124.70.

Thanks in advance,

Anna



.

Those are not the same number!

Try:

Console.WriteLine("{0:F2}", 12470 * 0.01);

Mike
 
Anna said:
True (BTW in my case 2470 is a long value).
But i need to convert it to a string value. So i need some string
formatting.

You have a number that is 100 times the number you want to display.
Therefore you have to divide it by 100 first. Unless the "/" key on your
computer is broken, you should be able to implement the solution Rick
gave you.
 
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 :) 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
 
Back
Top