小数处理-取整、保留小数位数
问了G老师和百度的各语言处理的demo。记录在此,方便以后使用时查阅(就不用频繁的问G老师了)。
向上取整:
在 JavaScript 中,可以使用 Math.ceil() 函数来实现向上取整:
let num = 3.14;
let roundedNum = Math.ceil(num);
console.log(roundedNum); // 输出 4
在 Java 中,可以使用 Math.ceil() 方法来实现向上取整:
double num = 3.14;
double roundedNum = Math.ceil(num);
System.out.println(roundedNum); // 输出 4.0
在 C# 中,可以使用 Math.Ceiling() 方法来实现向上取整:
double num = 3.14;
double roundedNum = Math.Ceiling(num);
Console.WriteLine(roundedNum); // 输出 4
在 SQL 中,可以使用 CEIL() 函数来实现向上取整:
SELECT CEIL(3.14);
这将返回 4。
向下取整:
JavaScript:
javascriptMath.floor(4.7); // 4
Java:
javaMath.floor(4.7); // 4.0
C#:
csharpMath.Floor(4.7); // 4
SQL:
sqlSELECT FLOOR(4.7); // 4
四舍五入:
JavaScript:
javascriptMath.round(4.7); // 5
Java:
javaMath.round(4.7); // 5
C#:
csharpMath.Round(4.7); // 5
SQL:
sqlSELECT ROUND(4.7); // 5
保留两位小数:
JavaScript:
javascriptMath.round(4.7777 * 100) / 100; // 4.78
Java:
javaMath.round(4.7777 * 100) / 100; // 4.78
C#:
csharpMath.Round(4.7777, 2); // 4.78
SQL:
sqlSELECT ROUND(4.7777, 2); // 4.78