473,799 Members | 3,111 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to get 0.000001 with printf?

Hello,

I'd like to print floating point numbers without trailing zeros.
I tried using %g, but now small numbers are printed in the %e
style ("Style e is used if the exponent from its conversion is
less than -4"). Is there some way to control this number (-4)?
I.e. can I get printf to print something like 0.0000001, but no
trailing zeros? Or should I do this by hand?

Thanks,
Mandar.

Nov 15 '05 #1
21 1997
On 27 Oct 2005 23:52:48 -0700, in comp.lang.c , "Mandar Mitra"
<ma**********@g mail.com> wrote:
Hello,

I'd like to print floating point numbers without trailing zeros.


I presume you mean you want 0.0100000 to print as "0.01" and
0.00001000 to print as "0.00001".

You need to roll your own code. You'd first of all have to work out
how many of the decimals were significant, then create a format string
via sprintf, then printf the number.
Working out how many decimals are significant is quite interesting.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 15 '05 #2
On 2005-10-28, Mark McIntyre <ma**********@s pamcop.net> wrote:
On 27 Oct 2005 23:52:48 -0700, in comp.lang.c , "Mandar Mitra"
<ma**********@g mail.com> wrote:
Hello,

I'd like to print floating point numbers without trailing zeros.


I presume you mean you want 0.0100000 to print as "0.01" and
0.00001000 to print as "0.00001".

You need to roll your own code. You'd first of all have to work out
how many of the decimals were significant, then create a format string
via sprintf, then printf the number.
Working out how many decimals are significant is quite interesting.


Probably the easiest way would be to simply truncate it to a given
amount, say, {FLT,DBL,LDBL}_ DIG and then cut off trailing zeros from
there.

A more exact, but slower, way, would be to try each amount of digits
in turn and see if the result of such compares equal to the
original.
Nov 15 '05 #3

Mandar Mitra wrote:
Hello,

I'd like to print floating point numbers without trailing zeros.


Right. You want to print .20000 as .2 but print
..19999 as .19999. This is a pretty stuipid thing to do,
so there is no built in way of doing it.
You need to do it by hand. Here's one way

-save the sign and convert to absolute value

-save the integer part of the numbers and
reduce them to less than 1.

- Round off your numbers to your desired maximum
number of digits.

- sprintf the number into a buffer, specifiying
the maximum number of digits

- remove trailing zeros (or replace with blanks
if your want constant field width)

- sprintf the integer parts, then strcat the fractional
parts (starting at the second character to lose
the zero)

-add the sign back to the front, (and pad with blanks
at the front if you want constant field width)
Normally, .2, .20, .200 mean different things, so supressing
the trailing zeros usually a BAD THING.

-William Hughes

Nov 15 '05 #4
On 2005-10-29, William Hughes <wp*******@hotm ail.com> wrote:
Normally, .2, .20, .200 mean different things, so supressing
the trailing zeros usually a BAD THING.


I can't think of any situation where they could mean different
things to C - and C itself does not provide for a way to easily
track significant figures.
Nov 15 '05 #5
In article <sl************ *******@random. yi.org>,
Jordan Abel <jm****@purdue. edu> wrote:
On 2005-10-29, William Hughes <wp*******@hotm ail.com> wrote:
Normally, .2, .20, .200 mean different things, so supressing
the trailing zeros usually a BAD THING.


I can't think of any situation where they could mean different
things to C - and C itself does not provide for a way to easily
track significant figures.


Ask yourself who *reads* the output of programs.

Hint: It isn't a C program (nor is it the C standard)

Nov 15 '05 #6
On 2005-10-29, Kenny McCormack <ga*****@yin.in teraccess.com> wrote:
In article <sl************ *******@random. yi.org>,
Jordan Abel <jm****@purdue. edu> wrote:
On 2005-10-29, William Hughes <wp*******@hotm ail.com> wrote:
Normally, .2, .20, .200 mean different things, so supressing
the trailing zeros usually a BAD THING.


I can't think of any situation where they could mean different
things to C - and C itself does not provide for a way to easily
track significant figures.


Ask yourself who *reads* the output of programs.

Hint: It isn't a C program (nor is it the C standard)


The format requested in this thread is analogous to the "general
number" format in programs such as Microsoft Excel. That it exists
and is the default tells me that there are certainly some situations
in which the _humans_ reading the output of the program will not
have a problem with supression of trailing zeros.
Nov 15 '05 #7
On Sat, 29 Oct 2005 15:38:15 GMT, in comp.lang.c ,
ga*****@yin.int eraccess.com (Kenny McCormack) wrote:
In article <sl************ *******@random. yi.org>,
Jordan Abel <jm****@purdue. edu> wrote:
On 2005-10-29, William Hughes <wp*******@hotm ail.com> wrote:
Normally, .2, .20, .200 mean different things, so supressing
the trailing zeros usually a BAD THING.


I can't think of any situation where they could mean different
things to C - and C itself does not provide for a way to easily
track significant figures.


Ask yourself who *reads* the output of programs.


Well, fundamentally .2, .20 and .200 cannnot actually mean different
things, so the difference is nothing more than cosmetic. What were you
thinking of?

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 15 '05 #8
In article <r1************ *************** *****@4ax.com>,
Mark McIntyre <ma**********@s pamcop.net> wrote:
On Sat, 29 Oct 2005 15:38:15 GMT, in comp.lang.c ,
ga*****@yin.in teraccess.com (Kenny McCormack) wrote:
In article <sl************ *******@random. yi.org>,
Jordan Abel <jm****@purdue. edu> wrote:
On 2005-10-29, William Hughes <wp*******@hotm ail.com> wrote:
Normally, .2, .20, .200 mean different things, so supressing
the trailing zeros usually a BAD THING.

I can't think of any situation where they could mean different
things to C - and C itself does not provide for a way to easily
track significant figures.


Ask yourself who *reads* the output of programs.


Well, fundamentally .2, .20 and .200 cannnot actually mean different
things, so the difference is nothing more than cosmetic. What were you
thinking of?


They mean very different things, as I am sure you are well aware.

Nov 15 '05 #9
Mark McIntyre <ma**********@s pamcop.net> writes:
On Sat, 29 Oct 2005 15:38:15 GMT, in comp.lang.c ,
ga*****@yin.int eraccess.com (Kenny McCormack) wrote:
In article <sl************ *******@random. yi.org>,
Jordan Abel <jm****@purdue. edu> wrote:
On 2005-10-29, William Hughes <wp*******@hotm ail.com> wrote:
Normally, .2, .20, .200 mean different things, so supressing
the trailing zeros usually a BAD THING.

I can't think of any situation where they could mean different
things to C - and C itself does not provide for a way to easily
track significant figures.


Ask yourself who *reads* the output of programs.


Well, fundamentally .2, .20 and .200 cannnot actually mean different
things, so the difference is nothing more than cosmetic. What were you
thinking of?


They certainly can mean different things. Depending on the context,
they can imply different levels of precision. ".2" can (sometimes)
mean anything from 0.15 to 0.25, whereas ".200" can mean anything from
0.1995 to 0.2005 (I'm ignoring the question of whether the endpoints
are included in the range).

Given:

double x = 0.2;
double y = 0.234;

"0.2" can be the result of printing either x or y; "0.200" cannot.

It's also valid to consider .2 and .200 both to represent the same
exact real value (one that's not representable in binary
floating-point). The interpretation depends on the context.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #10

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

Similar topics

2
5992
by: m_evergreen | last post by:
I would like the numbers 000001 to 999999 to autogenerate in a new database. I will be transfering information from another database and in that database the numbers 000001 to 010000 are already taken. They are used as identifiers in other programs and it would be easier if they were stored as written. Using identity the 0's are eliminated. Is there a way to keep them? Thank you, M
11
5958
by: Grumble | last post by:
Hello, I have the following structure: struct foo { char *format; /* format string to be used with printf() */ int nparm; /* number of %d specifiers in the format string */ /* 0 <= nparm <= 4 */ };
8
2721
by: aditya | last post by:
hi, Can anybody please tell me that how the following printf(...) statement works- main(){ int d=9; printf("%d",printf("%d")); return 0;
7
96338
by: teachtiro | last post by:
Hi, 'C' says \ is the escape character to be used when characters are to be interpreted in an uncommon sense, e.g. \t usage in printf(), but for printing % through printf(), i have read that %% should be used. Wouldn't it have been better (from design perspective) if the same escape character had been used in this case too. Forgive me for posting without verfying things with any standard compiler, i don't have the means for now.
188
17459
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
4
2013
by: pai | last post by:
Hi , Can any one tell me how this statement of printf is behaving . how the last digit is printed int a=2,b=4,c=7; printf("%d",printf("%d %d:",a,b)); //answer to this was 2 4:3
11
3927
by: timmu | last post by:
Someone asked me a question about integer division and printf yesterday, I tell him he should do a casting to float/double before you do any interger division. But he doesn't think so, so I try to do some example to explain, However, after some trying, I confused when I try to do some integer constant division, I know I should do float division something like 3.0/6.0, but I'm still confused where the -0.124709 comes for the following...
34
16002
by: Old Wolf | last post by:
Is there any possible situation for printf where %hd causes a different result to %d, and the corresponding argument was of type 'short int' ?
1
8521
by: linq936 | last post by:
Hi, I read in many places that the string to be outputted by printf() must be ending with newline, for example, it should be printf("Hello World.\n"); instead of printf("Hello World.");
0
9688
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10260
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10243
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10030
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6809
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5590
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4146
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 we have to send another system
2
3762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.