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
124 views
in Technique[技术] by (71.8m points)

How to cap and round number in ruby

I would like to "cap" a number in Ruby (on Rails).

For instance, I have, as a result of a function, a float but I need an int.

I have very specific instructions, here are some examples:

If I get 1.5 I want 2 but if I get 2.0 I want 2 (and not 3)

Doing number.round(0) + 1 won't work.

I could write a function to do this but I am sure one already exists.

If, nevertheless, it does not exist, where should I create my cap function?

question from:https://stackoverflow.com/questions/884512/how-to-cap-and-round-number-in-ruby

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

1 Answer

0 votes
by (71.8m points)

Try ceil:

 1.5.ceil => 2
 2.0.ceil => 2

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

...