Printf Question

M

Mandragon03

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
 
V

Victor Bazarov

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

V
 
M

Mandragon03

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

V

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

Juha Nieminen

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"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Question on printf with %x 8
printf & scanf order 7
printf format 6
Dynamic Printf 11
How to fix this code? 1
Customizing printf 23
Z-Index/Drop-down menu issues 2
printf magic? 2

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top