Formatting Numbers

  • Thread starter Thread starter Markus
  • Start date Start date
M

Markus

Hi all,

two questions on formatting numbers:

- Given is a decimal/money datatype in SQL-Server, e.g.
decimal(9,4), thus displaying a value like 13.2000

How can I prevent to display the unnecessary zeros at the end?
Is it only possible with a custom Formatter? Or is there a built-in
functionality in the Framework? I just found decimal.ToString("F2"),
which forces e.g. 2 numbers after the decimal-point.

My desired result as an example:
value -> display
13.2000 -> 13.2
13.0000 -> 13
13.2220 -> 13.222
...

- How can I format a number to fill zeros at the beginning?

Again an example, which shows my desired results:
value -> display
1 -> 00001
202 -> 00202
14321 -> 14321
...

thx for any input
Markus
 
Cor,


thanks for your input, but as I said, I just found something to format
all numbers the same way (e.g. all with 2 digits after the
decimal-point) with the standard NumberFormatInfo / IFormatProviders of
the Framework.
My question is: How can I solve exact my two cases, for which I did not
find any hints in the framework documentations & internet searches.

thx
Markus
 
Hi Markus,

I believe you would need a custom formatter for the first case, as you want
to remove the '.' if the decimal is a whole number.

As for the second, assuming you're working with an integer data type, the
numeric format string "00000" would provide you with the result you want.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.
 
Kevin,
I believe you would need a custom formatter for the first case, as
you want to remove the '.' if the decimal is a whole number.

Ok, thanks for the information.

As for the second, assuming you're working with an integer data type,
the numeric format string "00000" would provide you with the result
you want.

That easy! Thanks, works nice!

Markus
 
Cor,
Although there are alternatives was my opinion that it was on the
page I have sent you.

Thanks for getting back again.

I have checked your links and also perused through your second link.
However, I didn't find a solution for my problem.

The solution your link offers (in my opinion, maybe I am too blind to
see more):
I can define, how many digits are displayed after the '.', but I cannot
make it variable depending on the value of the number.

NumberFormatInfo nfi = new CultureInfo( "en-US", false ).NumberFormat;
nfi.NumberDecimalDigits = 4;
decimal x = 13.4300;
x.ToString("N", nfi);

This will display 13.4300, but I want it to be displayed 13.43. If x is
13.4322, then it should display 13.4322. So only the really required
digits should be displayed.


Anyways, thanks for all your help

Markus
 
13.2000 -> 13.2
13.0000 -> 13
13.2220 -> 13.222

You can also use the TrimEnd string function like in
MyNumber.ToString("F8").TrimEnd('0').TrimEnd('.')
1 -> 00001
202 -> 00202
14321 -> 14321

Here you could use the PadLeft string function like in
MyNumber.PadLeft(4,'0')

/LM
 
Thx to all for your input!
My desired result as an example:
value -> display
13.2000 -> 13.2
13.0000 -> 13
13.2220 -> 13.222

I think for problem no.1 I will use Luc's suggestions, but packaged in a
custom Formatter.

value -> display
1 -> 00001
202 -> 00202
14321 -> 14321

And for this one, Kevin's ToString("00000") just works fine!


thx again
Markus
 
I think for problem no.1 I will use Luc's suggestions, but packaged in a
custom Formatter.

A man after my own heart!

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.
 
Back
Top