If I have 2.55
, how do I write only .55
and skip 2 in programming language?
-
`number - floor(number)` – Weather Vane Sep 23 '15 at 16:37
-
What's the expected output when the number is negative? – R Sahu Sep 23 '15 at 16:39
-
@WeatherVane shouldn't that be `trunc(number)` in case `number` is negative? – Dmitri Sep 23 '15 at 16:43
-
@Dmitri `trunc` is non-standard. But I take the point, if negative `number - ceil(number)` – Weather Vane Sep 23 '15 at 16:44
-
1@WeatherVane Isn't `trunc()` standard as of c99? – Dmitri Sep 23 '15 at 16:52
-
@Dmitri you are right. – Weather Vane Sep 23 '15 at 16:54
-
2OP why accept the the answer with output of `"0.55"` when `".55"` was the required output in the post? – chux - Reinstate Monica Sep 23 '15 at 17:30
-
possible duplicate of [How to separate float into an integer and a fractional part?](http://stackoverflow.com/questions/23993898/how-to-separate-float-into-an-integer-and-a-fractional-part) – Mark Dickinson Sep 23 '15 at 18:39
4 Answers
Well you can do this to store it in another variable -
double a=2.55,b;
b =a-(long)a; // subtracting decimal part from a
printf("%.2f\n",b);
As pointed out by Mark Dickinson Sir in comment that this is not safe . So you can make use of function modf
from <math.h>
-
For example -
double a=-2.55,b,i;
b =modf(a,&i); // i will give integer part and b will give fraction part
printf("%.2f\n",b);

- 16,489
- 2
- 26
- 41
-
2As with @Mureinik's answer, this gives undefined behaviour for large `double`s. – Mark Dickinson Sep 23 '15 at 17:17
-
Use double modf(double value, double *iptr)
to get the factional part. Use round()
to get the best value near the requested precision.
double GetDecimalPlaces(double x, unsigned places) {
double ipart;
double fraction = modf(x, &ipart);
return fraction;
// or
double scale = pow(10.0, places);
return round(fraction * scale)/scale;
}
void GetDecimalPlaces_Test(double x, unsigned places) {
printf("x:%e places:%u -->", x, places);
printf("%#.*f\n", places, GetDecimalPlaces(x, places));
// Additional work needed if leading '0' is not desired.
}
int main(void) {
GetDecimalPlaces_Test(2.55, 2);
GetDecimalPlaces_Test(-2.55, 2);
GetDecimalPlaces_Test(2.05, 2);
GetDecimalPlaces_Test(0.0, 2);
GetDecimalPlaces_Test(0.0005, 2);
}
Output
x:2.550000e+00 places:2 -->0.55
x:-2.550000e+00 places:2 -->-0.55
x:2.050000e+00 places:2 -->0.05
x:0.000000e+00 places:2 -->0.00
x:5.000000e-04 places:2 -->0.00

- 143,097
- 13
- 135
- 256
One dirty trick is to cast your double
to an int
to get only the whole number. You can then subtract the two to get only the decimal part:
double d = 2.55;
double remainder = d - (int)d;
printf ("%.2f\n", remainder);

- 297,002
- 52
- 306
- 350
-
5This gives undefined behavour for large `d`, as a result of the cast to `int` overflowing. (C99 6.3.1.4p1: "If the value of the integral part cannot be represented by the integer type, the behavior is undefined.") Why not use the `modf` function from the math library? – Mark Dickinson Sep 23 '15 at 17:09
double
values are not perfectly precise, so small rounding errors can get introduced. You can store the total number in an Integer. You can for example divide by 100 to get the value before the .
and use % modulus to get the decimal values.
Example:
int main()
{
int num = 255;
printf("%d.%d\n", num / 100, num % 100); // prints 2.55
printf(".%d", num % 100); // prints .55
return 0;
}
This fails with negative numbers, but you can easily add cases to handle that.

- 10,685
- 6
- 35
- 62
-
1OP did not mention "money" (although he did not expand on any particular use as well). Nevertheless, your sugessted code fails for negative numbers and `num = 101` (although both can be trivially fixed). – Jongware Sep 23 '15 at 17:28
-
@Jongware `num = 101` will display `.1` as expected, or did you mean that it doesn't display `.01` ? – Andreas DM Sep 23 '15 at 17:45
-
Detail: `double` _values_ are precise. It is `double` math that introduces the rounding errors. – chux - Reinstate Monica Sep 23 '15 at 17:59