# Thread: Scientific notation

1. ## Scientific notation

Hey how can i convert scientific notations ??

if i use float in my program

is it possible to change it without casting ????

3. Code:
```#include <iostream>
using namespace std;

int main()
{
char againKel;
cout << "\n================================ Kelvin Converter ==============================\n\n" ;

do {
cout << "Enter &#176;K (Kelvin) degree to convert :";
float kTemp2Conv; // Kelvin Temperature to Convert

if (cin >> kTemp2Conv) {

float kCelsius = kTemp2Conv - 273.15;
float kFahrenheit = (kTemp2Conv * 1.8) - 459.67;
float kRankine = kTemp2Conv * 1.8;

cout << "\nAnswer: " << kTemp2Conv << " K (Kelvin) in Celsius scale = " << kCelsius;
cout << "\nAnswer: " << kTemp2Conv << " K (Kelvin) in Fahrenheit scale = " << kFahrenheit;
cout << "\nAnswer: " << kTemp2Conv << " K (Kelvin) in Rankine scale = " << kRankine;
cout << "\n\n";
}
else {
cout << "\nINVALID INPUT !";
cout << "\nPlease enter numerical values.\n";
}
cin.clear();
cin.ignore(100, '\n');
cout << "\nDo you want to convert temperature again ?";
cout << "\nEnter Y for yes, N for no.";
cout << "\nEntering wrong option other than Y/N will result in getting back to \nConvert Temprature menu.";
cout << "\n[y]/[n]:";
cin >> againKel;

} while (againKel == 'y'|| againKel == 'Y');
}```
run the program and enter 273.15

and then look at the answer of celsius

273.15 K= 0 C

but it shows me a very long answer

-6.10352e-06 this is what i Got now

is there any way to convert the scientific digit ?

4. In this case, what you are showing is the "rounding error", which is caused by the fact that floating point numbers don't have infinite precision, so when you mathematically operate on the values, some decimal poins down from your actual value may be remaining in the number. If you set the precision for printing, it will print only so many decimal places. Look up the "iomanip" methods on the iostreams, and you'll find several useful functions.

--
Mats

and found this

setprecision()

is this the right functio ?

or fixed()?

6. You can also try using double everywhere instead of float or changing your calculations to explicitly use floats instead of double:
Code:
```float kCelsius = kTemp2Conv - 273.15f;
float kFahrenheit = (kTemp2Conv * 1.8f) - 459.67f;
float kRankine = kTemp2Conv * 1.8f;```
You should be getting warnings about the above lines in your original code, some warnings should be treated with greater attention.

7. but what about those ? functions i mentioned in my earlier post aren't they valid ?

is this code working ??
Code:
```// modify basefield
#include <iostream>

#include <iomanip>
using namespace std;

int main () {
double a,b,c;
a = 3.1415926534;
b = 2006.0;
c = 1.0e-10;
cout.precision(5);
cout       <<         a << '\t' << b << '\t' << c << endl;
cout <<   fixed    << a << '\t' << b << '\t' << c << endl;
cout << scientific << a << '\t' << b << '\t' << c << endl;
return 0;
}```

Popular pages Recent additions