1

If I have 2.55, how do I write only .55 and skip 2 in programming language?

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Joe Woods
  • 73
  • 3
  • 9

4 Answers4

2

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);
ameyCU
  • 16,489
  • 2
  • 26
  • 41
2

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
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
1

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);
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 5
    This 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
1

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.

Andreas DM
  • 10,685
  • 6
  • 35
  • 62
  • 1
    OP 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