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.

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
*/``````
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.