Dan*_*Lee 51
你在这里使用整数.尝试对计算中的所有数字使用小数.
decimal share = (18m / 58m) * 100m;
Run Code Online (Sandbox Code Playgroud)
Pet*_*nov 23
18 / 58 是整数除法,结果为0.
如果要进行十进制除法,则需要使用十进制文字:
decimal share = (18m / 58m) * 100m;
Run Code Online (Sandbox Code Playgroud)
Kra*_*ime 10
由于有些人从计算结果为0的任何线程链接到这个,我将其添加为解决方案,因为并非所有其他答案都适用于案例场景.
需要对各种类型进行计算以便获得该类型的概念适用,但是上面仅显示"十进制"并使用它的简短形式,例如作为18m要计算的变量之一.
// declare and define initial variables.
int x = 0;
int y = 100;
// set the value of 'x'
x = 44;
// Results in 0 as the whole number 44 over the whole number 100 is a
// fraction less than 1, and thus is 0.
Console.WriteLine( (x / y).ToString() );
// Results in 0 as the whole number 44 over the whole number 100 is a
// fraction less than 1, and thus is 0. The conversion to double happens
// after the calculation has been completed, so technically this results
// in 0.0
Console.WriteLine( ((double)(x / y)).ToString() );
// Results in 0.44 as the variables are cast prior to calculating
// into double which allows for fractions less than 1.
Console.WriteLine( ((double)x / (double)y).ToString() );
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
33297 次 |
| 最近记录: |