Hi experts, I'm trying to convert a string to a double.

double d = atof("245.76");

The d variable become 245.75999999999999.

I need to have a double 245.76000000000000.

How suggest to fixit?

double d = atof("245.76");

The d variable become 245.75999999999999.

I need to have a double 245.76000000000000.

How suggest to fixit?

#include <iostream>

int main(void) {

double d = atof("245.76");

std::cout << d << std::endl;

return 0;

}

I get the correct result.

In any case, if you want exact precision, then a double is probably not what you want (it's a floating point number).

What do you need this for ? For certain applications, fixed point could be an option ...

Numbers in the computer are usually stored in binary format. For a floating-point number, it's a binary fraction with a binary power of two scale factor. It's unlikely a decimal number will be exactly representable.

But you can round it-- use a print format with two decimal places and the print function will round the number to two decimal places.

Don't think so. The problem is the convertion to a double. If you would read the variable to a string and only convert for calculations you won't get the problem. Another 'solution' is to store the count of the fractional digits (of course you don't need to store it if you *know* that it is always 2) and make an appropriate rounding when outputting the number. Again, the problem is an output problem only. 245.75999999999999 is as good as 245.76 if used for calculations. For output you can do that:

#include <iomanip>

#include <iostream>

using namespace std;

double d = atof("245.76");

cout << fixed << setprecision(2) << d << endl;

You also could set the precision generally for cout by

cout.precision(2);

If fixed or scientific is set, setprecision determines the number of fractional digits (digits right of the period). If not it determines the number of significant digits (5).

Regards, Alex

Then I don't really understand your problem. Can you post your code that converts the string "245.76" to a double and when showing the double, it gives "245.75999999999999" ?

Anyway, like grg said : a double has only about 15 digits of precision (which is enough for most applications). If you need more precision, then you'll need to use a custom datatype instead.

As I asked earlier : what do you use this for ?

This one is on us!

(Get your first solution completely free - no credit card required)

UNLOCK SOLUTION
Anyway the problem here seems to be in the output function.

Alex ( itsmeandnobodyelse ) proposed something like:

cout << fixed << setprecision(2) << d << endl;

This is OK if the input string represents exactly 2 digits after the floating point, but if there are more, the output will be rounded.

A more general way to display the correct value of a floating point number in C++ would be:

double d = atof("245.769");

std::cout.setf(std::ios::f

std::cout << d << std::endl;

This way the output is not rounded at all.

This one is on us!

(Get your first solution completely free - no credit card required)

UNLOCK SOLUTION
>>>> std::cout << d << std::endl;

with that d was rounded to 6 significant digits (default).

>>>> this way the output is not rounded at all.

That isn't true. Take

double d = atof("1245.769");

and you will see that the output is 1245.77.

double d = atof("0.76999999");

gives 0.769999 as output.

Or worse:

double d = atof("245.76124599999999")

std::cout.setf(std::ios::f

std::cout << d << std::endl;

would round to 245.76 !!!

So, with format flags you only can decide whether you want rounding dependent on the number of fractional digits or dependent on significant digits. If you only want to round differences that came from conversion decimal to binary and vice versa, you need to set the precision to 14 (significant digits) and set the format to floatfield.

double d = atof("245.76124599999999")

std::cout.setf(std::ios::f

std::cout.precision(14);

std::cout << d << std::endl;

So, only the combination of floatfield and precision(14) will give a correct result..

Regards, Alex

You may have overread that he suggests, *Round* variable, multiply variable by 100 and store as integer.

You can do rounding by adding 0.5 before multiplying. Then 123.455 would turn to 123.460 and the integer*100 is 12346, correctly rounded. 123.544 would give 123.549 and finally 12354, what is correct as well. So 0, 1, 2, 3, 4 for the digit to round would be rounded down while 5, 6, 7, 8, 9 would round up. Following that the algorithm is

int r100 = (int)((d +0.005)*100);

For output you need

cout << r100/100 << '.' << r100%100;

It isn't easier or more transparent than

cout << fixed << precision(2) << d;

Regards, Alex

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy

Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif

Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster

CTP, Sr Infrastructure Consultant

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Connect with Certified Experts to gain insight and support on specific technology challenges including:

- Troubleshooting
- Research
- Professional Opinions

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.