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

c# - how to convert Math.Ceiling result to int?

Math.Ceiling returns double because double may store much bigger numbers. However if i'm sure that int type is capable to store the result how should I convert? Is it safe to cast (int) Math.Ceiling(... ?

question from:https://stackoverflow.com/questions/8832978/how-to-convert-math-ceiling-result-to-int

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

1 Answer

0 votes
by (71.8m points)

If you are sure that you do not cross the capacity of int, it should be perfectly safe to do

int myInt = (int)Math.Ceiling(...);

If you are not sure about the bound, you could go with long instead of int.


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

...