473,386 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Formatting Numbers

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
9 2800
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
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
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
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
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
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
> 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
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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Dan Sommers | last post by:
Hi, I have a class whose objects represent physical quantities including uncertainties and units, and I would like more control over the way they print. I have a __str__ method which outputs...
4
by: Tommi Mäkitalo | last post by:
Hi I need to format floating-point-numbers with exact 2 digits after decimal point. I could use printf with "%.2f", but it don't use std::locale. Any ideas? -- Tommi Mäkitalo
4
by: Dave Brydon | last post by:
Access 2003 I have a combo box in my personnel table, which draws its data from a trade code table; the original field in the code table, is numeric, Long Integer, and formatted with 5 zero's . ...
4
by: John Sutor | last post by:
I need some code that, on each keyup event, will take all of the numbers typed into the text box and format as they type to look like this 100 1,000 10,000 100,000 1,000,000 John S
7
by: ilona | last post by:
Hi all, I store phone numbers in the database as 123447775665554(input mask is used for input, and some numbers have extensions), and I also know from db if the number is Canadian, US, or some...
5
by: Carla | last post by:
I have Access 2000. I made a mailing list database and for the life of me can't remember how to format the zip code field so it will print on the label as xxxxx-xxxx. I have tried various - 9, #,...
6
by: Rafael Olarte | last post by:
The goal of this project is to output the following information as follows: 34.5 38.6 4.1 42.4 3.8 close 46.8 4.4 big change. The values of the first colunm are obtain from a file...
2
by: jerryyang_la1 | last post by:
I need some advice on formatting numbers using PHP. My database holds numbers like: 10 0.2 I need these to be displayed as 10.00
3
by: sparks | last post by:
We have one database that they are constantly reformatting their inputs. I asked about the changes and they can never get the same types of numbers from the people. some are -.95 and later they...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.