English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

when having a double result how can i have .50 print out inmy result. for example 1.50 or 2.5o or 3.50 but i only want the .50 printed out, not the entire number just the decimal

2007-03-20 12:51:21 · 4 answers · asked by Anonymous in Computers & Internet Programming & Design

4 answers

float d = 1.50
float e = d - ceil(d);
printf("%f",e)

2007-03-21 01:15:49 · answer #1 · answered by Anonymous · 0 1

Example:
Floatig Number as for instance double d1 = 3.5
1.) Cast into integer from float or double:
int n = d1; //results in n = 3
2.) Subtract d1 from n results in 3.5 - 3 = 0.5:
double d2 = d1 - n; //resulting in d2 = 0.5
If you wish only to display ".5" instead of "0.5", then you can convert it into a string and remove the first index of the string; namely, the zero "0".

2007-03-20 20:06:06 · answer #2 · answered by Coosa 2 · 1 1

#include

cout.precision(2) ;
cout << 45.678 << endl;


/** GOOD LUCK **/

2007-03-21 01:01:32 · answer #3 · answered by Liviawarty J 2 · 0 1

double num=3.05;
printf("%.2f",num);

2007-03-21 08:16:06 · answer #4 · answered by Rishi 3 · 0 0

fedest.com, questions and answers