|
|
|
Rounding up decimals in C?
|
Original Message
|
Name: glasskoi
Date: July 15, 2003 at 20:13:12 Pacific
Subject: Rounding up decimals in C? OS: Win98SE CPU/Ram: AMD XP 2000+, 512MB
|
Comment: I feel silly for asking this, but.. how do I round up decimals in C? My textbook asks me to use numbers rounded to 2 decimal places for my current project, but it has never explained how to do so. My copy of C for Dummies doesn't have 'rounding' or anything similar in its index, and I can't find anything about it in the section on decimals. And I haven't been able to get a response from my professor. Does C have a function or something for this? Or maybe there's some trick with an IF statement that I'm just not getting? I was thinking it might be that, but I can't figure out what I would tell it for the IF expression, how to indicate that I want it to test the third decimal position.. though I know for incrementing, I just have to tell it to add .01 to the number in question if the third position is 5 or more. But anyway, I can't figure out how I would tell it to check that third position, and aside from that possibility, I'm totally stumped. Anybody know how to do this with C?
Report Offensive Message For Removal
|
|
Response Number 1
|
Name: elric
Date: July 15, 2003 at 20:38:02 Pacific
|
Reply: (edit)G'day, You might look for a trunc or mod statement, but I think it can be done using formatting. Look at your options for printf()and the %d options.I might not be exactly right but I think that you can use something like %d2 to specify 2 decimal places. regards Elric
Report Offensive Follow Up For Removal
|
|
Response Number 2
|
Name: glasskoi
Date: July 15, 2003 at 20:42:11 Pacific
|
Reply: (edit)Well, the problem I'm having is that I can truncate at 2 decimal places, but not round up.. So in this case, making a loan payment schedule program, when I try to get my program to mimic the textbook's sample data, my total payment (principal plus all interest paid) is off by something like ten cents because the book is rounding up to 2 places for all its calculations, while my program is just using truncated values. It's just not accurate enough this way..
Report Offensive Follow Up For Removal
|
|
Response Number 3
|
Name: JackG
Date: July 15, 2003 at 21:22:20 Pacific
|
Reply: (edit)Lets see. If I have a value like 22.xxxx and I always wanted to round any fractional part up to a whole number value, I would first add 0.9999 and then truncate (or MOD) the result to get a rounded up value. But that is not quite how interest calculations work, they either round up or round down depending on how much the fraction is.
Report Offensive Follow Up For Removal
|
|
Response Number 4
|
Name: Don Arnett
Date: July 15, 2003 at 21:48:15 Pacific
|
Reply: (edit)Normally, for rounding you add 1/2 the value of the smallest position and then truncate. So if you want to round to 2 decimal places, you'd add .005 and then truncate. 10.233432 + .005 = 10.238432 -> 10.23 10.238432 + .005 = 10.243432 -> 10.24
Report Offensive Follow Up For Removal
|
|
Response Number 5
|
Name: JackG
Date: July 15, 2003 at 21:55:02 Pacific
|
Reply: (edit)Answering his home work question for him? His professor would not respond because the homework questions are designed to make him think logically for himself, not just get the "right answer". And to learn how to solve logical problems, not just look up answers in the book or on the internet. If one can not logically solve such simple programming problems with a few hints and clues, they will never make it as a programmer.
Report Offensive Follow Up For Removal
|
|
Response Number 6
|
Name: Don Arnett
Date: July 15, 2003 at 22:10:13 Pacific
|
Reply: (edit)I gave him the algorithm for rounding. I didn't write any code for him. And as I look back at his question, I didn't even answer the question that he asked.
Report Offensive Follow Up For Removal
|
|
Response Number 7
|
Name: glasskoi
Date: July 15, 2003 at 22:34:43 Pacific
|
Reply: (edit)First, not *too* important, but it's 'her', not 'him'.. JackG, I *want* to figure this stuff out for myself, as much as I can. Honestly, I prefer it that way--it gives me much more a sense of pride and accomplishment to know that I earned my grade with my own hard work in a class. I would never ask the people here to write code for me--that's what *I'm* meant to be doing, and I *want* to do it. But in this case, the textbook is asking me to do something that it hasn't explained how to do, and this being my first attempt at C, I really didn't know if might be some built-in function for rounding. The entire rest of the loan payment schedule program already works. The only thing that was missing is properly rounded currency values. And if you look at my original post, I said that I was wondering if there was some trick involving the third decimal place--I was already close to the answer, I just wasn't quite sure how to proceed from that beginning. I'm sorry if I sound a little defensive about this, but I've worked really hard in this course. Coming to this board actually requires me to swallow my pride; it takes several hours of tearing my hair out over a program before I can accept that I just really need to ask for help. So far, I've asked three times, including tonight. The module I've asked for help with is module 20, my 20th assignment. Each assignment typically contains three or four different tasks. So if you figure I've asked for help with three items out of sixty (or more) and currently have an A in the course.. I'd have to say I think I'm doing pretty good. I'd also like to say that I *chose* to do this loan payment project.. the professor said those of us who found this one too hard could do a different project, printing out patterns based on a user-specified number of rows and columns. I chose this because I wanted to learn something useful. Anyway.. again, I'm sorry for sounding defensive. If I could do all of it without any help, I'd rather do that. In this case, I needed to ask for help, and I'm pleased that I was at least on the right track to the answer. Thank you very much for your help, Don. As before, I really appreciate it and will give you credit again in my header comments.
Report Offensive Follow Up For Removal
|
|
Response Number 8
|
Name: SN
Date: July 15, 2003 at 23:07:32 Pacific
|
Reply: (edit)SN does not miss the irony that DON, of all people, is being accused of being too much help on a homework assignment. -SN
Report Offensive Follow Up For Removal
|
|
Response Number 10
|
Name: glasskoi
Date: July 16, 2003 at 07:56:16 Pacific
|
Reply: (edit)I know how to round. It was, as you say, wizard, learned in my math course. My question here was whether or not C had a special function (like sqr or pow) which is used for rounding. Maybe it *is* a mistake for me to ask for help here, after all. Maybe I've gotten the wrong impression, and Don and borelli34 and SN and egkenny, who were all kind enough to help me, are actually the exceptions to the rule here. I said I had to swallow my pride to come here and ask for help, but that doesn't mean I don't have any at all when I post here. I will have to think seriously about whether it's worth it to ask for help again in the future, or if I'd rather have a bad grade but know that people like some of the ones posting on this thread are not looking down on me. Thank you to those people who have helped me. Credit was given to you, as promised. I really appreciate that you shared your knowledge with me. I will work hard in the course, because I want to, but also to honor the help you gave me. Goodbye, at least for a while.
Report Offensive Follow Up For Removal
|
|
Response Number 11
|
Name: Don Arnett
Date: July 16, 2003 at 08:24:12 Pacific
|
Reply: (edit)glasskoi I wouldn't worry too much about the people who are having a bad day/life and taking it out on you. It's very easy, in this anonymous atmosphere, to be very short sighted, negative and critical. We tend to not remember that there is a real live, warm blooded person with real feelings on the other side of the post. When you forget this, it is easy to be negative. And there are also those who are just jerks and will be overly negative just because... I say this having been on the jerk side of the line many times myself. I now try to have the attitude now that if I can't say something helpful, to not say anything at all. I don't always follow that ideal, but when I do try to point out that someones question is ridiculous, I try to do so without calling them an idiot or whatever. So, is the name 'glasskoi' a reference to a glass fish?? Isn't a koi a type of fish found in ornamental ponds?
Report Offensive Follow Up For Removal
|
|
Response Number 12
|
Name: egkenny
Date: July 16, 2003 at 18:01:41 Pacific
|
Reply: (edit)This algorithm will round Number to four decimal places Result = truncate(Number * 10000 + 0.5) / 10000 You will have to substitute the proper C syntax. You must make sure you use the correct C function to truncate both positive and negative numbers correctly. With a little thought you could write a function to round to any number of decimal places.
Report Offensive Follow Up For Removal
|
|
Response Number 13
|
Name: moneo
Date: August 20, 2003 at 19:10:36 Pacific
|
Reply: (edit)I was looking for accounting standards regarding rounding when I came across this page and subject. There exists an IEEE floating-point standard which has been incorporated into many computer programming languages, affecting the assigning of floating point numbers (with decimals) to an integer variable. This operation will "round" the floating point number before storing it into the integer. However, if the number ends in .5 for example, 2.5 is stored into the interger as 2, but 3.5 is stored as 4. The standard rounds odd numbers differently than even numbers. Another problem is that the IEEE standard also rounds negative numbers differently that positive numbers. These IEEE differences do not comply with common accounting practice regarding rounding. Can you imaging explaining to a CPA that your program rounds even/odd numbers and positive/negative numbers differently? Ok, then what I recommend is: 1) Never assign floating point numbers into integer variables expecting proper rounding, and don't use any built-in functions in your language that claim to do this, like the CINT (Convert to Integer) function in Basic. 2) Perform rounding with the following code. Please excuse the Basic code, I haven't written C in 8 years and I'm rusty: N is assumed to be defined as a floating point number. ROUNDED.RESULT = SGN(N) * INT(ABS(N) + .5) The above code takes the following into consideration: * The absolute or ABS assures that the number to work with is positive. * This positive number is rounded by the rounding factor of "+.5". * The integer value INT is taken on the above positive result. * The sign of the original input number is restored by multiplying by the sign or SGN which is 1 for positive, -1 for negative, and 0 for zero. You can provide scaling of the number into the code depending on where the decimal point lies, like in one of the previous messages. Note: I'm still looking for an accounting authority source to back this up. I'm speaking from 42 years of programming experience.
Report Offensive Follow Up For Removal
|

|

|
Use following form to reply to current message:
|
|

|