Hello.

I'm stuck on a part in my program. Some numbers are adding up properly, others aren't. I assume the problem is that I'm using integers instead of doubles, however, how would you format the following in C?

How to describe it, it's like a chain. I have to take a percentage of an initial value and add it to the next number. Then take a percentage of the next number and add that to the next number. The numbers get pretty big, and the percentages vary, 5%, 10%, 30%. For the most part, everything works out nice and well, but theres a few numbers that are off by 1, either +1 or -1. If I try to add something like +0.5 after the statement it throws off other numbers. So, logically it seems reasonable I'd need to switch from ints to doubles, but that confuses me as to how the output will look. The output cannot be in decimal form. Can you take a double number and print it out as an integer would look?

Heres how the current int output looks:

printf("%4d", solution);

The 4 space format is necessary. Is there a way to take a type double number, say, 5.32 and make it appear on the output as just "5" yet still be formatted with the 4 spaces?

Hello.

I'm stuck on a part in my program. Some numbers are adding up properly, others aren't. I assume the problem is that I'm using integers instead of doubles, however, how would you format the following in C?

How to describe it, it's like a chain. I have to take a percentage of an initial value and add it to the next number. Then take a percentage of the next number and add that to the next number. The numbers get pretty big, and the percentages vary, 5%, 10%, 30%. For the most part, everything works out nice and well, but theres a few numbers that are off by 1, either +1 or -1. If I try to add something like +0.5 after the statement it throws off other numbers. So, logically it seems reasonable I'd need to switch from ints to doubles, but that confuses me as to how the output will look. The output cannot be in decimal form. Can you take a double number and print it out as an integer would look?

Heres how the current int output looks:

printf("%4d", solution);

The 4 space format is necessary. Is there a way to take a type double number, say, 5.32 and make it appear on the output as just "5" yet still be formatted with the 4 spaces?

Yes, it is called casting. Consider the following:

double d1 = 5.21;
int num;
num = (int)d1;
printf("%.2d", (double)num);

Output = 5.00

Also, I wrote a decimal-to-integer function that allows you to convert decimal-to-integer rounding it off no matter how long the float portion of the number is.
Link to snippet: http://www.daniweb.com/code/snippet652.html
Code:

int Double2Integer(char *fnum) {     
  int integeri = atoi(fnum),i,j,k;     
  char hex = 0x30;     
  i=k=0;      
  while (fnum[i] != '.') i++;    
     j = strlen(fnum);     
     for (;j>i;j--) {     
       if (fnum[j] >= 5 && fnum[j] <= 9) {                 
        while (fnum[j-1] != hex)                    
           hex++;                
           hex++;                
 fnum[j-1] = hex;      
 }     
}     
if (fnum[i+1] >= 5 && fnum[i+1] <= 9)       
integeri++;                
return integeri; 
}

The only downfall is that you have to conver the double to a
string of characters (i.e char a[] = "5.5421"). If that doesn't
answer you question, let me know.

Good luck, LamaBot

You could also do something like this:

printf("%4d", (int)ceil(solution - 0.5));

This'll print the rounded value without actually changing the double, so your other numbers should be fine.

#include <math.h> for the ceil function

Infraction's way works as well. Here is just a simple way to convert from double to char to accommodate the latter option:

char dblBuf[8];
sprintf(dbBuf,"%4.2f",solution);
printf("%4d",Double2Integer(dbBuf));

It is good to have options if a given option is good in and of itself.

Good luck, LamaBot

This article has been dead for over six months. Start a new discussion instead.