Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Printf Question

Reply
Thread Tools

Printf Question

 
 
Mandragon03@gmail.com
Guest
Posts: n/a
 
      04-16-2007
I am looking for a way to take a floating point number and get rid of
any extraneous 0's at the end. For instance if I have

myFloat = 9999;

printf("%f", myFloat);

I get 9999.0000. I want to get rid of these 0's.

Here is one catch. I also need to be able to do this:

myFloat = .531;

printf("%f", myFloat);

I get .5310000

I need to get .531.

I realize I can do this

printf("%2f", myFloat);

which will limit the mantissa to two places, but this will not work
for what i am doing because I can not lose that much precision on
floats like .05369.

Thank you for you time,

Mandragon03

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-17-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I am looking for a way to take a floating point number and get rid of
> any extraneous 0's at the end. For instance if I have
>
> myFloat = 9999;
>
> printf("%f", myFloat);
>
> I get 9999.0000. I want to get rid of these 0's.
>
> Here is one catch. I also need to be able to do this:
>
> myFloat = .531;
>
> printf("%f", myFloat);
>
> I get .5310000
>
> I need to get .531.
>
> I realize I can do this
>
> printf("%2f", myFloat);
>
> which will limit the mantissa to two places, but this will not work
> for what i am doing because I can not lose that much precision on
> floats like .05369.


The problem actually is rather simple. The default is 7 decimal
digits in the result, which means that 0.5310000 and 0.530999951
will give you the same output. The zeros are there to indicate to
what digit the output was _rounded_ to.

If you really need to lose the trailing zeros, you will have to
trim them yourself. Convert your number to a string and drop all
chars from it until the first non-zero:

std:stringstream s;
s << myFloat;
std::string str = s.str();
while (str.size() > 1 && *str.rbegin() == '0')
str.erase(str.size() - 1);

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      04-17-2007
On Mon, 16 Apr 2007 20:40:32 -0400, "Victor Bazarov"
<(E-Mail Removed)> wrote in comp.lang.c++:

> (E-Mail Removed) wrote:
> > I am looking for a way to take a floating point number and get rid of
> > any extraneous 0's at the end. For instance if I have
> >
> > myFloat = 9999;
> >
> > printf("%f", myFloat);
> >
> > I get 9999.0000. I want to get rid of these 0's.
> >
> > Here is one catch. I also need to be able to do this:
> >
> > myFloat = .531;
> >
> > printf("%f", myFloat);
> >
> > I get .5310000
> >
> > I need to get .531.
> >
> > I realize I can do this
> >
> > printf("%2f", myFloat);
> >
> > which will limit the mantissa to two places, but this will not work
> > for what i am doing because I can not lose that much precision on
> > floats like .05369.

>
> The problem actually is rather simple. The default is 7 decimal


Correction, 6 decimal places.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
Reply With Quote
 
Mandragon03@gmail.com
Guest
Posts: n/a
 
      04-17-2007
On Apr 16, 6:40 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > I am looking for a way to take a floating point number and get rid of
> > any extraneous 0's at the end. For instance if I have

>
> > myFloat = 9999;

>
> > printf("%f", myFloat);

>
> > I get 9999.0000. I want to get rid of these 0's.

>
> > Here is one catch. I also need to be able to do this:

>
> > myFloat = .531;

>
> > printf("%f", myFloat);

>
> > I get .5310000

>
> > I need to get .531.

>
> > I realize I can do this

>
> > printf("%2f", myFloat);

>
> > which will limit the mantissa to two places, but this will not work
> > for what i am doing because I can not lose that much precision on
> > floats like .05369.

>
> The problem actually is rather simple. The default is 7 decimal
> digits in the result, which means that 0.5310000 and 0.530999951
> will give you the same output. The zeros are there to indicate to
> what digit the output was _rounded_ to.
>
> If you really need to lose the trailing zeros, you will have to
> trim them yourself. Convert your number to a string and drop all
> chars from it until the first non-zero:
>
> std:stringstream s;
> s << myFloat;
> std::string str = s.str();
> while (str.size() > 1 && *str.rbegin() == '0')
> str.erase(str.size() - 1);
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask


I figured as much. Thank you for taking the time to answer my
question. You guys are great =)

 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      04-18-2007
(E-Mail Removed) wrote:
> I am looking for a way to take a floating point number and get rid of
> any extraneous 0's at the end. For instance if I have
>
> myFloat = 9999;
>
> printf("%f", myFloat);
>
> I get 9999.0000. I want to get rid of these 0's.


Have you tried "%g" instead of "%f"?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
printf affects following printf/s azza C Programming 0 10-17-2010 09:43 AM
Extracting printf(...) from (void) printf(....) guru Perl Misc 8 02-03-2009 10:37 PM
(void) printf vs printf whatluo C Programming 29 09-08-2005 05:42 PM
bus error with printf line included, error without printf line? ben C Programming 4 06-26-2004 04:42 PM
Question about evaluating the arguments of printf (maybe OT?) Edith Gross C++ 2 11-02-2003 12:16 PM



Advertisments