Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
296 views
in Technique[技术] by (71.8m points)

cmath - Rounding up and down a number C++

I'm trying to allow my program to round a number up and down respectively.

For example, if the number is 3.6, my program is suppose to round up the nearest number which is 4 and if the number is 3.4, it will be rounded down to 3.

I tried using the ceil library to get the average of 3 items.

results = ceil((marks1 + marks2 + marks3)/3)

However, the ceil only rounds the number down but does not roll the number up.

There's 1 algorithm i stumbled upon

var roundedVal = Math.round(origVal*20)/20;

but i still can't figure a formula for some problem.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
std::ceil 

rounds up to the nearest integer

std::floor 

rounds down to the nearest integer

std::round 

performs the behavior you expect

please give a use case with numbers if this does not provide you with what you need!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...