HOW TO ROUND UP FLOATING POINT NUMBER? like 2.34 to 2.35. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

HOW TO ROUND UP FLOATING POINT NUMBER? like 2.34 to 2.35.

https://code.sololearn.com/c1k0nyAjDY5M/?ref=app

4th Nov 2022, 12:51 PM
Aamin Patel
Aamin Patel - avatar
24 Answers
+ 4
Apply ceil() function. Import math.h header.
4th Nov 2022, 1:18 PM
Jayakrishna 🇮🇳
+ 3
Jayakrishna🇮🇳 OP needs to round up the number, your approach won't work if it the last decimal digit be less than 5. round() rounds to nearest integer, whether up or down. ceil() also may not work depending on OP's requirements. Aamin Patel I guess you need to round up the value in a range of 0.05 steps, am I right? that is not something that you have a pre-made function for that and you have to implement the algorithm yourself (it's relatively easy, first try to do it yourself). You can't simply round 2.08 to 2.10 because if you do it with ceil() or ceilf() (multiplying by 100 then deviding by 100) it would probably be 2.09. and if you use round() on 7.04 , you'll get 7.0 So think about it and try to implement the algorithm. also remember that floats (for example 2.07) most of the time are actually something like 2.072835... not 2.0700000 (because of the way they are processing in base 2) and they are trouble full to work with.
5th Nov 2022, 10:34 AM
Tina
Tina - avatar
+ 3
Aamin Patel Ok. If you want exact to correct solution, share your attempt here if you need.. So that helps where the code leading to wrong. General ways to try is already I mention. You can adapt those to your requirements.. There exist 3 built in functions, floor(), ceil(), round() which can used depends on the context. It's working I think you already know. May their rounding failure is depends your logic. Try double type instead of float.
6th Nov 2022, 12:03 PM
Jayakrishna 🇮🇳
+ 3
All I was pointing out was comparing your solutions to the OP's requirements even at very vague and unclear level of question Jayakrishna🇮🇳 but even then, OP asks for rounding up and round() doesn't round up always. I'm sure you've read its man page. Pointing out the ceil() I upvoted your post. Now you bring up a typo.... Was it any good for anyone that you bring up a typo? "Op not mentioned 7.03->7.05" "7.04=>7.05" what's the difference? 7.03, 7.04.... does it have any important value to be mentioned here? finally read your own post again, if you know how a program should work for some values "manually" and for some others perhaps automatically, please let me know.
6th Nov 2022, 12:26 PM
Tina
Tina - avatar
+ 3
Please be as precise and provide as much details as you can Aamin Patel when asking question. Also show your efforts. almost 70% of your code are not needed to solve that question AND if you get wrong answer, your post title should be like: code coach kelaidoscop test case fails. Not how to round these numbers!
6th Nov 2022, 1:05 PM
Tina
Tina - avatar
+ 3
The problem and all details you want which i write in this program see it and give advice Jayakrishna🇮🇳 https://code.sololearn.com/chxTw76D9Lal/?ref=app
6th Nov 2022, 4:49 PM
Aamin Patel
Aamin Patel - avatar
+ 3
Thankyou so much for help me😊 Jayakrishna🇮🇳 & Tina 🕊🇺🇦🇮🇷 both of you are very supportive, i glade to have you guys for help 😊✨.Keep supporting people's will motivate from you.
6th Nov 2022, 6:11 PM
Aamin Patel
Aamin Patel - avatar
+ 2
Tell me how to round up floating point number like 5.48 to 5.50 2.08 to 2.10 7.04 to 7.05 ???
4th Nov 2022, 1:42 PM
Aamin Patel
Aamin Patel - avatar
+ 2
Tina 🕊🇺🇦🇮🇷 Thanks for insight.. May you not seen my first reply is about "rounding up" Function. And second reply is about "according to OP examples ". I generally don't give readymade solutions, because it won't help. I try to give a sample way to solve. So that help OP try again and discuss further if any need. And don't understand your reply is same as OP question. First 2 of OP examples can be easily made with sample that I given. 2.08 to 2.10 is easily rounded as printf("%.2f", round(2.08*10) /10) ; //2.10 And printf("%.2f", round(5.48*10) /10) ; // 5.50 But 7.04 to 7.05 is not rounding up value. You need manually done. My reply will depends on OP reply as I can understand then level of understanding. Next am waiting OP response for what actually exact requirement, if it unsolved... I thought that is enough to identify his solution as he already aware of round up by format specifier.
6th Nov 2022, 8:39 AM
Jayakrishna 🇮🇳
+ 2
We can try only if you provide full details.. Which code coach task, description and how are you trying? Your attempt..! For your examples, I replied my solution already..
6th Nov 2022, 10:23 AM
Jayakrishna 🇮🇳
+ 2
I'm not willing to start another long and pointless argument with you Jayakrishna🇮🇳 but from your answers: "2.08 -> 2.10" // ok "5.48 -> 5.50" // ok "But 7.04 -> 7.05 is not rounding up" then what is it? "You need manually done" How? like in the middle of program execution OP says: wait, I have to do this one manually using paper or calculator? However about the exact definition of problem itself, I'm agree with you and Aamin Patel should've provided the full details which he hasn't yet. in my previous reply (read it carefully) I *assumed* based on code name, "shopping bill", "rounding up" (not just rounding) & examples, a coefficient of 5 required & from 2 decimal digits I guessed it must be rounding up in 5 cents so for example $5.33 would be $5.5 and $7.03 -> $7.05 So I said *IF* that's what you want, there's an algorithm for it, but try to do it yourself instead of searching on the net because I won't give ready made code either it's not a favor but a betrayal to OP. I hope we all are clear now.
6th Nov 2022, 11:26 AM
Tina
Tina - avatar
+ 2
Is it replying pointless argument? Ok then what is it? Tina 🕊🇺🇦🇮🇷 "But 7.04->7.05 is not rounding up" then what is it? "You need manually done" How? like in the middle of program execution OP says: wait, I have to do this one manually using paper or calculator?" What point you want raise and why? About my first answer also? Op not mentioned 7.03->7.05 7.04=>7.05 As you said, OP not mentioned full details, I also made assumption and related replies.. It those not at all relavent ? It there any irrelavent to be pointed out ? Depending on example, I assumed that there making adjustments, not all roundups, so that can be done with function type manual coding which returns desired value. (Not mean pen and paper use of.., no need that much sarcasm reply.., always don't take as negetive talk, it is pointless.. I just always reply depends on question, depends on reply towards me.. Point to point.) And about your reply, I just asked "If mine not works, then your reply is a working one? ".
6th Nov 2022, 11:56 AM
Jayakrishna 🇮🇳
+ 2
Tina 🕊🇺🇦🇮🇷 I didn't find it a typo, not noticed it even. Just said for clarity, only as "Op not mentioned 7.03->7.05" "7.04=>7.05" , not even pointed any. ( I thought OP getting 7.04 , but need 7.05 , there are chances for 7.0477 can be 7.05 or 7.04 but not 7.03 to 7.05) If this is not good? Then : My first reply for you is just a reply of your response. I said thanks for it also. So that you should not understand wrong way. I have no intentions then but seems you intentionally looking for loopholes on my answers!!? I was thought same as "Was it good to point out "it's won't work in a negative way? " I think this is not good to lead wrong way.. What I "Manually" means : you can write a your own function like instead of predefined or built in function searching.. Here built-in functions, may not work for all cases.. Let me know, what's wrong in my second reply in this thread actually..? It does not mean reply must fully answer.. Is relevant is not enough?
6th Nov 2022, 4:21 PM
Jayakrishna 🇮🇳
+ 2
Aamin Patel I think you need to apply discount on grant total (cost1, not on cost). Try this or Use double type for accurate precision values.. Edit: https://www.sololearn.com/Discuss/3085069/?ref=app
6th Nov 2022, 4:44 PM
Jayakrishna 🇮🇳
+ 2
Aamin Patel As I already told, use double type for variable tax, and cost. edited to work as said. #include <stdio.h> #include<math.h> int main() { float input; scanf("%f",&input); // int is enough double cost = input*5.00; double tax = cost*7/100; cost += tax; if( input > 1) cost -= cost *10/100; printf ("%.2lf\n",cost); return 0; }
6th Nov 2022, 5:47 PM
Jayakrishna 🇮🇳
+ 2
Okay i'll do it, Jayakrishna🇮🇳
6th Nov 2022, 6:08 PM
Aamin Patel
Aamin Patel - avatar
+ 2
Hope it solved..!? You're welcome Aamin Patel
6th Nov 2022, 6:16 PM
Jayakrishna 🇮🇳
+ 1
To number of digits : you can use format specifier like printf("%.2f", num); // for decimal places Other way : Multiply number with 10^(number of digits), apply round function then again divide by 10^(number of digits). Ex: 1 digits: printf("%.2f", round(5.48*10) /10) ; //=> 5.50 // num upto for 2 decimal places : printf("%.2f", round(num*pow(10, 2)) /pow(10, 2) );
4th Nov 2022, 4:22 PM
Jayakrishna 🇮🇳
+ 1
There's a code coach which requires this type of rounding float number, and i want a proper way, if you answer me it's very helpful for me and you guys also couldn't found answer, keep trying and search 🔎.
6th Nov 2022, 9:56 AM
Aamin Patel
Aamin Patel - avatar