• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 98
  • Last Modified:

C++ Scientific Notation

Using math.h
double value;
value = 1.97* pow(10,-4);

Open in new window


Returns 1.97e-04

I am looking for .000197 to be what is returned and I'm not sure where I'm going wrong.
0
Computer Guy
Asked:
Computer Guy
  • 2
3 Solutions
 
Dave BaldwinFixer of ProblemsCommented:
You need to use 'printf' or 'sprintf' to get it to be formatted like that.  Normal math routines will not keep all those leading zeros.
0
 
phoffricCommented:
I happen to also like printf or sprintf. But there are C++ purists who do not. If you are one of them, then take a look at this.
http://www.cplusplus.com/reference/ios/scientific/
Following sample code from this link shows how to format according to your needs.
// modify floatfield
#include <iostream>     // std::cout, std::fixed, std::scientific

int main () {
  double a = 3.1415926534;
  double b = 2006.0;
  double c = 1.0e-10;

  std::cout.precision(5);

  std::cout << "default:\n";
  std::cout << a << '\n' << b << '\n' << c << '\n';

  std::cout << '\n';

  std::cout << "fixed:\n" << std::fixed;
  std::cout << a << '\n' << b << '\n' << c << '\n';

  std::cout << '\n';

  std::cout << "scientific:\n" << std::scientific;
  std::cout << a << '\n' << b << '\n' << c << '\n';
  return 0;
}

Open in new window

Possible output:

default:
3.1416
2006
1e-010

fixed:
3.14159
2006.00000
0.00000

scientific:
3.14159e+000
2.00600e+003
1.00000e-010

Open in new window

1
 
sarabandeCommented:
like printf or sprintf. But there are C++ purists who do not.

printf or sprintf may not only avoided by purists but also by programmers who want to make their software safer, better readable, and less error-prone.

safer: the format string usual provided as literal could be used by malware to hijack the process and run malware code with the access rights of the process. another risk is that the printf statement can be used for a buffer-overflow attack.

better readable: beside of simple formats those statements are an imposition for anyone who has to maintain such code.

error-prone: the compiler neither can check the format nor the arguments. when using sprintf there is no way to validate that the buffer is big enough.

std::cout << "fixed:\n" << std::fixed;

you may use std::setprecision(n) to get n significant digits (rounded at the n-th digit) if used without std::fixed or to get n digits after the decimal point if used with std::fixed.

Sara
0
 
sarabandeCommented:
As the question was posted in the C++ TA, the comment of phoffric showing c++ code samples is the solution.

However, printf and sprintf are widely used to convert and format Integer and Floating Point numbers also in C++ code. therefore Dave Baldwin's suggestion and my comment may serve as a helpful addition to the solution.

Sara
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now