By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,745 Members | 1,897 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,745 IT Pros & Developers. It's quick & easy.

Formatting Numbers

P: n/a
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
Mar 20 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Markus,

Have a look at the Iformatprovider

http://msdn.microsoft.com/library/de...classtopic.asp

I hope this helps,

Cor
"Markus" <di***************@THISyahoo.de> schreef in bericht
news:%2****************@tk2msftngp13.phx.gbl...
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

Mar 20 '06 #2

P: n/a
Cor,
Have a look at the Iformatprovider
http://msdn.microsoft.com/library/de...classtopic.asp

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
Mar 20 '06 #3

P: n/a
Markus,

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

http://msdn.microsoft.com/library/de...igitstopic.asp

There were more links.

I hope this helps,

Cor
"Markus" <di***************@THISyahoo.de> schreef in bericht
news:eV**************@TK2MSFTNGP12.phx.gbl...
Cor,
Have a look at the Iformatprovider
http://msdn.microsoft.com/library/de...classtopic.asp

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

Mar 20 '06 #4

P: n/a
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.

"Markus" <di***************@THISyahoo.de> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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

Mar 20 '06 #5

P: n/a
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
Mar 20 '06 #6

P: n/a
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
Mar 20 '06 #7

P: n/a
> 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
Mar 20 '06 #8

P: n/a
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
Mar 21 '06 #9

P: n/a
> 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.

"Markus" <di***************@THISyahoo.de> wrote in message
news:ev**************@TK2MSFTNGP10.phx.gbl... 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

Mar 21 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.