Hello All
I am new at this website and I hope I can be a good memeber.
I have a question about C programing language.

I need to change the decimal point of a double for example:
If I have a doubles with 5 decimal digits

123.45678
234.12345
34.34567

I want to be able to change them to only 2 decimal point , for example:

123.45
234.12
34.34

or to 3 decimal point:
123.456
234.123
34.345

I am doing some testing and I want to investigate the result of more decimal digit in the final result. Is there any C function which does that??

Or Is there any easy way to do this??

Thank you guys.

Dani AI

Generated

Short take: printing with printf only changes how a number is shown. To actually do arithmetic “as if” a value were 12.34 instead of 12.34567 you must change the representation used for the calculation. raised that distinction, and while ’s quick cast trick is a useful hack, it can misbehave for negative values, very large magnitudes and because binary floating‑point can’t represent many decimal fractions exactly.

A robust pattern is fixed‑point (integer) arithmetic: pick scale = 10^N (100 for two decimals), convert each value to an integer by scaling and applying your chosen policy (truncate or round), do all arithmetic on integers, then divide by the scale only when you need a floating result or output. Using a 64‑bit integer (int64_t) avoids overflow for typical ranges and removes binary‑fraction surprises during intermediate math. Example (truncate to 2 decimals):

#include <stdint.h>
#include <math.h>

/* truncate */
int64_t cents = (int64_t)floor(value * 100.0 + 1e-9);
int64_t sum = cents + other_cents;
double result = (double)sum / 100.0;

Bits vs decimal digits: to guarantee d decimal digits you need at least ceil(d * log2(10)) bits (log2(10) ≈ 3.32193). So 2 decimal digits ≈ 7 bits (1 byte), 4 digits ≈ 14 bits (2 bytes). A double’s significand is 53 bits, which gives roughly 15–16 decimal digits of precision — that’s about the maximum number of meaningful decimal digits you can expect from double.

Practical advice: for money use integer cents or a decimal library (decNumber, libmpdec) instead of double. If you stay with double, use round()/trunc()/llround() or fixed‑point conversion before doing important calculations, and be explicit about how negatives and ties are handled.

Recommended Answers

All 5 Replies

#include <stdio.h>

int main(void)
{
   double value[] = {123.45678,234.12345,34.34567};
   int j;
   for ( j = 2; j <= 3; ++j )
   {
      size_t i;
      printf("%d decimal places:\n", j);
      for ( i = 0; i < sizeof value / sizeof *value; ++i )
      {
         printf("%.*f\n", j, value[i]);
      }
   }
   return 0;
}

/* my output
2 decimal places:
123.46
234.12
34.35
3 decimal places:
123.457
234.123
34.346
*/

great, so there is no built in function but I can use this simple routine and change the percision.
But This is just priniting the values out.
I need to do calculation with this values, for example if value is 12.34567
then I need to do my calculation with just 12.34

Mutliply by 100; cast to int to truncate, then divide by 100 back to a double.

#include <stdio.h>
#include <math.h>

int main(void)
{
   double value[] = {123.45678,234.12345,34.34567};
   int j;
   for ( j = 2; j <= 3; ++j )
   {
      size_t i;
      printf("%d decimal places:\n", j);
      for ( i = 0; i < sizeof value / sizeof *value; ++i )
      {
         double scale = pow(10.0, j);
         double result = (int)(value[i] * scale) / scale;
         printf("result = %g\n", result);
      }
   }
   return 0;
}

/* my output
2 decimal places:
result = 123.45
result = 234.12
result = 34.34
3 decimal places:
result = 123.456
result = 234.123
result = 34.345
*/

great. thank you sooo much, I can not belive you guys give answer this quick.:):)
another question?

I have tried this for 4 digit of decimal digit and I get the wrong results.
Why is that?? Can I extend this to 4 digit or 5 digit??

Another question is that, If we just want to save a double with 2 dicimal digit, then how many bits do we need?? in other words, what is a relationship between number of digits in dicimal and number of bytes that we use for it. do I make any sense?? :)


Can you suggest a good book or a good website which talks about the socket programing using MFC??

#include <stdio.h>
#include <math.h>

int main(void)
{
   double value[] = {123.45678,234.12345,34.34567};
   int j;
   for ( j = 2; j <= 6; ++j )
   {
      size_t i;
      printf("%d decimal places:\n", j);
      for ( i = 0; i < sizeof value / sizeof *value; ++i )
      {
         double scale = pow(10.0, j);
         double result = (int)(value[i] * scale) / scale;
         printf("result = %f\n", result);
      }
   }
   return 0;
}

/* my output
2 decimal places:
result = 123.450000
result = 234.120000
result = 34.340000
3 decimal places:
result = 123.456000
result = 234.123000
result = 34.345000
4 decimal places:
result = 123.456700
result = 234.123400
result = 34.345600
5 decimal places:
result = 123.456770
result = 234.123440
result = 34.345660
6 decimal places:
result = 123.456779
result = 234.123449
result = 34.345669
*/

What Every Computer Scientist Should Know About Floating-Point Arithmetic
Beej's Guide to Network Programming

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.