勤奋菜鸟的天堂

导航

除法运算小数点的问题

在c#中int与int的除法默认不保留小数点,

float result = 1010 / 100; // result = 10;

需要保留小数点,可以如下

float result = 1010f或者(float)1010F / 100;

如果需要限定小数点的位数,则可以使用Math.Round():

float result = Math.Round((float)x/ y,2); 后面的2表示保留小数点后2位小数

posted on 2013-02-01 14:50  勤奋的菜鸟  阅读(340)  评论(0编辑  收藏  举报