Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert scientific notation to decimal in C++

I want to be able to output in decimal and not scientific for all cases in my code.

If I have 122041e+08 then I want it to display as 122041000

If I have 4.6342571e+06 then I want it to display as 4634257.1

... and so on.

Using my code, the output for 4.6342571e+06 is 4634257.100000

void conversion(double counts)
{
  std::ostringstream ss;
  ss << std::fixed << counts;
  std::cout << ss.str() << " MeV";
}

Can someone explain to me why it adds 0's to the end and if it's possible to remove them.

like image 416
Robert Whitley Avatar asked Feb 23 '12 10:02

Robert Whitley


People also ask

How do you convert scientific notation to decimal?

Convert scientific notation to decimal formDetermine the exponent, n , on the factor 10 . Move the decimal n places, adding zeros if needed. If the exponent is positive, move the decimal point n places to the right. If the exponent is negative, move the decimal point |n| places to the left.

How do you convert to a decimal?

To convert from percent form to decimal form, drop the percent sign and move the decimal point two places to the left. Example: Write 5.2% in decimal form.


1 Answers

You can use std::setprecision.

like image 152
Oliver Charlesworth Avatar answered Oct 01 '22 08:10

Oliver Charlesworth