Hey how can i convert scientific notations ??
if i use float in my program
is it possible to change it without casting ????
Printable View
Hey how can i convert scientific notations ??
if i use float in my program
is it possible to change it without casting ????
example, please?
run the program and enter 273.15Code:#include <iostream>
using namespace std;
int main()
{
char againKel;
cout << "\n================================ Kelvin Converter ==============================\n\n" ;
do {
cout << "Enter °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');
}
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 ?
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
well after reading your post I googled :p
and found this
setprecision()
is this the right functio ?
or fixed()?
You can also try using double everywhere instead of float or changing your calculations to explicitly use floats instead of double:
You should be getting warnings about the above lines in your original code, some warnings should be treated with greater attention.Code:float kCelsius = kTemp2Conv - 273.15f;
float kFahrenheit = (kTemp2Conv * 1.8f) - 459.67f;
float kRankine = kTemp2Conv * 1.8f;
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;
}