Division in C# to get exact value [duplicate]

痞子三分冷 提交于 2019-12-01 21:17:08

try:

 double result = (double)150/100;

When you are performing the division as before:

double result = 150/100;

The devision is first done as an Int and then it gets cast as a double hence you get 1.0, you need to have a double in the equation for it to divide as a double.

Cast one of the ints to a floating point type. You should look into the difference between decimal and double and decide which you want, but to use double:

double result = (double)150 / 100;
double result = (150.0/100.0)

One or both numbers should be a float/double on the right hand side of =

Mayank

Make the number float

var result = 150/100f

or you can make any of number to float by adding .0:

double result=150.0/100

or

double result=150/100.0

If you're just using literal values like 150 and 100, C# is going to treat them as integers, and integer math always "rounds down". You can add a flag like "f" for float or "m" for decimal to not get integer math. So for example result = 150m/100m will give you a different answer than result = 150/100.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!