X除以Y,最小为1

Ste*_*ong 1 javascript c# math

当我将x = 20除以y = 7,20/7 = 2.857时,我将按原样得到结果.

但是如果结果小于1,我会取1,所以

15/20 = 1

现在我可以这样做:

If x > y then
   result = x / y
else
   result = 1
Run Code Online (Sandbox Code Playgroud)

有一个更好的方法吗?

小智 5

试试以下

var x = 20.0;
var y = 7.0;

var result = (x > y) ? x / y : 1;
Run Code Online (Sandbox Code Playgroud)