| | |
Newbie question
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2005
Posts: 20
Reputation:
Solved Threads: 0
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:
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?
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:
C Syntax (Toggle Plain Text)
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:
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?C Syntax (Toggle Plain Text)
printf("%4d", solution);
c Syntax (Toggle Plain Text)
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:
c Syntax (Toggle Plain Text)
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
Last edited by Lazaro Claiborn; Mar 9th, 2007 at 2:32 pm.
You could also do something like this:
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
C Syntax (Toggle Plain Text)
printf("%4d", (int)ceil(solution - 0.5));
#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:
It is good to have options if a given option is good in and of itself.
Good luck, LamaBot
c Syntax (Toggle Plain Text)
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
Last edited by Lazaro Claiborn; Mar 9th, 2007 at 5:53 pm.
![]() |
Similar Threads
- Newbie Question - How to generate a link (PHP)
- Redirecting - Newbie Question (PHP)
- Basic scripting question (Getting Started and Choosing a Distro)
- HttpContext.Current Question (VB.NET)
- newbie... Java load large picture :) Lit'l help plez (Java)
- Newbie Question - Hiding php extension (PHP)
- Stupid Newbie Question (C)
- AGP Aperture Size Question (Windows NT / 2000 / XP)
- How to network two Win98 machines (Networking Hardware Configuration)
Other Threads in the C Forum
- Previous Thread: can you help me in this program
- Next Thread: Project
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators initialization iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list lists locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql odf open opensource openwebfoundation owf pattern pdf performance pointer pointers posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling scripting segmentationfault send shape socketprograming socketprogramming stack standard string suggestions systemcall test testautomation unix urboc user voidmain() wab win32api windows.h






