## C Programming Tutorial – 36 – Rounding Numbers

GitHub – https://github.com/buckyroberts
reddit – https://www.reddit.com/r/thenewboston/
Support – https://www.patreon.com/thenewboston
thenewboston – https://thenewboston.com/

• Views:107,616 views
• Rating:
• Categories:
• Tags:

NIBII I Iir says:

i love your videos ,but i don’t like bacon

Mantas says:

sorry i have new fix and more easy:

int main()
{
using namespace std;
double n,v,k,m,k1, r;
n = 100,67; //
v = 4.26; //
m = 20; //

k = n – m;
r = k / v;

cout << "NOT Rounded= " <<  r << endl;     cout << "Rounded= " <<  round ( r )<< endl;               system ("pause");     return 0;

Ezhrule Salgeraz says:

how do I can convert this into c++??

TheEjaay says:

Note: Always name a float bacon1

humbiiiiiiiiiiiiiiii says:

there’s the round() function that rounds the number like we were taught in elementary

SangoProductions213 says:

if you want it to round as we normally do, then do Floor(number+0.5), and that will round it up if it is .5 or above, or down if it’s .4 or below. You can also do Ciel(number-0.5) but who likes subtraction?
For example: you have 3.5 this would round to 4 right? Well, let’s plug it in. Floor(3.5+0.5) = Floor(4.0) = 4.0.
And for 3.1? Floor(3.1+0.5) = Floor(3.6) = 3.0

Alex K says:

how to round up to the nearest 10th?

miiworld2 says:

How come I get a compiler error? I wrote the exact same code as you did.
Yet I keep getting the undefined reference to both floor and ceil.

Mantas says:

the most EASY WAYS TO ROUND THE NUMBER IS:
#include
#include
#include
#include
#include

{
flour m = 45,8494
cout << "Answer is rounded number:" << round ( m ) << endl; system ("pause"); return 0; } /////THE END

Harishankar Ravindran says:

coz, why the heck not ? and @bucky, what happened to the chick ? your friend’s hot friend ?

-1 for forgetting to mention the round() function which also exists in math.h.

nathnolt says:

-1 for saying that the computer isn’t as smart as smart. as stated by Sebastian, it can round to the nearest whole number. Even if round wasn’t available, you could use floor(float + 0.5) for positive values.

clickclaxer01 says:

Geez Bucky! Go to bed! You have work in the morning!

Steffan says:

There is a round() function in the math.h library that does normal rounding. You don’t have to specify if you want to round up or down.

Swapnil G. says:

Thanks!

Mantas says:

flour = flout sorry and after 45.8494 is ; =45.8494;

Ignacio says:

Bucky you are the man

beavermosh1 says:

Ceil stands for ceiling, like a roof.

Hari Kiran Vusirikala says:

link or video how to round decimal numbers?

yark park says:

Idk how you make coding interesting/funny but you do

I think it works better 🙂

#include
#include
#include

int main()
{
float a = 9.45;
printf(“Enter Number “);
scanf(“%f”,&a);

float b = floor(a);
if(a-b<0.25 & a-b<0.0)
{
b+=0;
}
else if(a-b>0.25 & a-b<0.5)
{
b+=0.5;
}
else if(a-b<0.5 & a-b>0.75)
{
b+=0.5;
}
else if(a-b>=0.75)
{
b+=1;
}

printf(“A %0.3f”,b );

return 0;
}

Osi famosi says:

Can someone tell me how to have these #include texts in my programs durable?

Endless Strictures says:

Mildlyhomestuck

Leo Nardy says:

how about i want to rounding up to nearest 0.5?
for example:
13.14 => 13.50
13.61 => 14.00
13.50 => 13.50

Danny_Programs ! says:

I rounded with floor this: 5.999999999999999999999999. It didn’t rounded down, it actually got me 6! You need at least 7 9’s after decimal place for it to round it up

zhu; dhk says:

Bucky why do you use floats instead of doubles?

floor= u know what it is
ceil= plaster of a roof

Rohit_Veer says:

Actually there is a Another Way to do this just type .0 in between % and f (%.0f) and this will Round the Numbers

soni blogs says:

Thanks for help me

Rayane says:

Hey guys here is my code, but I want to know if there is a command that allows to test the second number after the comma in a number ( ex: 9.6 : test the “6” if it’s above or less than 5 ). that will make it more efficient:

int main()

{
float num1 = 9.4;

if(num1>=9.5){
printf(“num1 is %.2f n”, ceil(num1));

}else{
printf(“num1 is %.2f n”, floor(num1));
}

return 0;
}

Tayyab Mazhar says:

just use “% . 1f” this will round up the number to 1 decimal point and it will do it the way we used to do it in classes. THIS IS SIMPLE AND BEST

Pedal Eve says:

I still cant debug my problem in greedy(program) of the value 4.2. what am I missing?