473,503 Members | 2,322 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Significant digits in double with Format strings?


MSDN documentation for format strings seems ambiguous. It's says
things like "significant digits or number of decimal places" all over
the place. Well those two things are different.

How do I show a specific number of significant digits to the right of
the decimal place with a format string? NOT number of decimal places,
significant digits.

For example;

if I have the number .001
the format string "{0:N2}" shows 0.00 yet if you were showing two
significant digits shouldn't that number display as 0.001?

another example is a number like 0.00123 showing 2 significant digits
would show 0.0012

It seems the number 2 in the format string is the number of decimal
places not the number of significant figures.

How can I use a format string to do this?

In my application the user can set the number of significant digits they
want to see.

thanks
mike
Mar 13 '07 #1
7 18558
Michael Howes <mh****@xfortebio.comwrote:
MSDN documentation for format strings seems ambiguous. It's says
things like "significant digits or number of decimal places" all over
the place. Well those two things are different.
Indeed.
How do I show a specific number of significant digits to the right of
the decimal place with a format string? NOT number of decimal places,
significant digits.

For example;

if I have the number .001
the format string "{0:N2}" shows 0.00 yet if you were showing two
significant digits shouldn't that number display as 0.001?
Yes - but MSDN for "standard numeric format strings" says that "N"
specifies the number of decimal places, not significant digits. "G"
specifies the number of significant places, and works fine.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 13 '07 #2
> For example;

if I have the number .001
the format string "{0:N2}" shows 0.00 yet if you were showing two
significant digits shouldn't that number display as 0.001?

Yes - but MSDN for "standard numeric format strings" says that "N"
specifies the number of decimal places, not significant digits. "G"
specifies the number of significant places, and works fine.
Ah...thanks!

the MSDN page I saw didn't say much about G. It just said "General",
so I sort of ignored that.
http://msdn2.microsoft.com/en-us/library/s8s7t687.aspx

phew, thanks...I didn't really want to write it and was more surprised
that I didn't think it was built it.

mike
Mar 13 '07 #3
>>
For example;

if I have the number .001
the format string "{0:N2}" shows 0.00 yet if you were showing two
significant digits shouldn't that number display as 0.001?

Yes - but MSDN for "standard numeric format strings" says that "N"
specifies the number of decimal places, not significant digits. "G"
specifies the number of significant places, and works fine.
One more thing. I noticed G doesn't pad 0s to the right

for example if I have the number .01 and use the format string {0:G2}
shouldn't the resulting string be 0.010?

Also how do I not have the format string add a 0 to the left of the
decimal? Most of the numbers I'm working with are less than 1

thanks
mike

Mar 13 '07 #4
Michael Howes <mh****@xfortebio.comwrote:
if I have the number .001
the format string "{0:N2}" shows 0.00 yet if you were showing two
significant digits shouldn't that number display as 0.001?
Yes - but MSDN for "standard numeric format strings" says that "N"
specifies the number of decimal places, not significant digits. "G"
specifies the number of significant places, and works fine.

One more thing. I noticed G doesn't pad 0s to the right

for example if I have the number .01 and use the format string {0:G2}
shouldn't the resulting string be 0.010?
No, not as far as I'm aware. For instance, 12 to 5 significant figures
is just 12, not 12.000. (IIRC!)
Also how do I not have the format string add a 0 to the left of the
decimal? Most of the numbers I'm working with are less than 1
Personally I think that 0.1 is a lot clearer than .1 - it's easy to
miss the .

I suspect you'd have to trim the 0 off yourself.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 14 '07 #5

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
Michael Howes <mh****@xfortebio.comwrote:
>if I have the number .001
the format string "{0:N2}" shows 0.00 yet if you were showing two
significant digits shouldn't that number display as 0.001?

Yes - but MSDN for "standard numeric format strings" says that "N"
specifies the number of decimal places, not significant digits. "G"
specifies the number of significant places, and works fine.

One more thing. I noticed G doesn't pad 0s to the right

for example if I have the number .01 and use the format string {0:G2}
shouldn't the resulting string be 0.010?

No, not as far as I'm aware. For instance, 12 to 5 significant figures
is just 12, not 12.000. (IIRC!)
Definitely not. 12.000 indicates accuracy of within .005, if not better,
while 12 could be off by 1 or more.
>
> Also how do I not have the format string add a 0 to the left of the
decimal? Most of the numbers I'm working with are less than 1

Personally I think that 0.1 is a lot clearer than .1 - it's easy to
miss the .

I suspect you'd have to trim the 0 off yourself.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Mar 14 '07 #6
On Mar 14, 4:59 pm, "Ben Voigt" <r...@nospam.nospamwrote:
for example if I have the number .01 and use the format string {0:G2}
shouldn't the resulting string be 0.010?
No, not as far as I'm aware. For instance, 12 to 5 significant figures
is just 12, not 12.000. (IIRC!)

Definitely not. 12.000 indicates accuracy of within .005, if not better,
while 12 could be off by 1 or more.
Yup, you're right. Looks like a bug then.

Jon

Mar 14 '07 #7
>>> for example if I have the number .01 and use the format string {0:G2}
>>>shouldn't the resulting string be 0.010?
No, not as far as I'm aware. For instance, 12 to 5 significant figures
is just 12, not 12.000. (IIRC!)
Definitely not. 12.000 indicates accuracy of within .005, if not better,
while 12 could be off by 1 or more.

Yup, you're right. Looks like a bug then.
anyone have ideas for ways around this problem?
I was trying to think of a way to maybe format twice?
I have a lot of numbers I try and show quickly so am not that into the
idea of writing my own loops to count decimals and sig figs

thanks
mike
Mar 15 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
4310
by: mrstephengross | last post by:
Hi all... How can I find out the number of significant digits (to the right of the decimal place, that is) in a double? At least, I *think* that's what I'm asking for. For instance: 0.103 --> 3...
2
6203
by: pw | last post by:
Running this on MSWin2000 PC, I show 6 only digits for float and double. i.e. l_float and l_double show 33333.1 (6 only digits). Is it possible to increase/alter this? // Test ints, floats...
8
18736
by: David Corby | last post by:
Hi everybody, I've got a problem. I'm trying to round a double to a particular number of significant digits, in this case 5, but I can't figure out a way around getting _exactly_ what I want...
3
2485
by: mrstephengross | last post by:
Hi all... How can I find out the number of significant digits (to the right of the decimal place, that is) in a double? At least, I *think* that's what I'm asking for. For instance: 0.103 -->...
2
3335
by: Ingo Nolden | last post by:
Hello, I want to format floats ( doubles ) into small strings of 7 characters. I tried to use std::ostream, but I don't get the desired behaviour. Especially in scientific notation I am not...
6
3997
by: Jonny | last post by:
If you run the following code , you will get 1.12346e07 as the output. What do I need to do if you want all the significant digits output, like 1.123456789e07? #include<iostream> using...
2
2344
by: mrshrinkray | last post by:
I feel a bit ashamed asking this after 6 years of C#, but is there a format string for significant figures before the decimal place? e.g. 2,354,856: 1 sf = 2,000,000 2 sf = 2,400,000 3 sf =...
0
7357
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7012
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5598
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1522
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
402
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.