如何在 TypeScript 中对数字进行四舍五入?
在本教程中,我们将学习如何在 TypeScript 中对数字进行四舍五入。在 TypeScript 中,数字数据类型可以包含带有小数部分的数字值,有时我们需要通过四舍五入来删除小数部分。
例如,如果您想在应用程序中显示某个任务的进度,并且获取了进度的小数部分,但只想显示整数部分,则需要对数字进行四舍五入。
在这里,我们将学习 3 到 4 种不同的四舍五入方法。
使用 Math.round() 方法对数字进行四舍五入
在 TypeScript 中,Math.round() 方法用于将数字四舍五入到最接近的小数点。返回四舍五入后的整数值。
语法
let number1: number = 322.3232; Math.round(number)
在上面的语法中,我们创建了 number 变量,并使用 math.round() 方法对数字进行四舍五入。
参数
Math.round() 方法接受一个参数,如下所述。
number − 它是一个需要四舍五入为最接近整数的数字值。
示例
在下面的示例中,我们创建了四个包含不同值的数字变量。之后,我们使用 Math.round() 方法对每个数值进行四舍五入。
在输出中,用户可以观察到,由于 number1 的小数部分更接近 322 而不是 323,因此它向下舍入为 322。与 number1 相同,number2 最接近 323,因此它向上舍入为 323。此外,number4 变量也被舍入为无穷大,因为它的值是无穷大。
let number1: number = 322.3232; // 小数部分接近零的数字 let number2: number = 0.0300012; // 介于 0 和 1 之间的数字 let number3: number = 322.98878; // 小数部分接近 1 的数字 let number4: number = Infinity; // 无穷大值 // 使用 Math.round() 方法对不同的数值进行取整 console.log( "After rounding the " + number1 + " it's value is " + Math.round(number1) ); console.log( "After rounding the " + number2 + " it's value is " + Math.round(number2) ); console.log( "After rounding the " + number3 + " it's value is " + Math.round(number3) ); console.log( "After rounding the " + number4 + " it's value is " + Math.round(number4) );
编译后,它将生成以下 JavaScript 代码 -
var number1 = 322.3232; // 小数部分接近零的数字 var number2 = 0.0300012; // 介于 0 和 1 之间的数字 var number3 = 322.98878; // 小数部分接近一的数字 var number4 = Infinity; // 无穷大值 // 使用 Math.round() 方法对不同的数字进行取整 console.log("After rounding the " + number1 + " it's value is " + Math.round(number1)); console.log("After rounding the " + number2 + " it's value is " + Math.round(number2)); console.log("After rounding the " + number3 + " it's value is " + Math.round(number3)); console.log("After rounding the " + number4 + " it's value is " + Math.round(number4));
输出
上述代码将产生以下输出 -
After rounding the 322.3232 it's value is 322 After rounding the 0.0300012 it's value is 0 After rounding the 322.98878 it's value is 323 After rounding the Infinity it's value is Infinity
使用 Math.trunc() 方法向下舍入数字
Math.trunc() 方法始终对作为参数传递的数字值进行向下舍入。在 TypeScript 中,我们也可以使用 Math.truc() 方法删除数字的小数部分。
语法
用户可以按照以下语法学习如何使用 Math.trunc() 方法向下舍入数字。
let var1: number = 100; Math.trunc(var1)
这里 var1 是一个需要向下舍入的值。
示例
在下面的示例中,我们定义了四个数字,并用不同的值初始化它们。我们使用了 Math.trunc() 方法对数字进行向下舍入,即删除小数部分。
在输出中,用户可以看到 var1、var2 和 var3 都是四舍五入到 100 的变量,因为该方法删除了小数部分。
// 具有不同值的数字变量 let var1: number = 100; let var2: number = 100.9999; let var3: number = 100.0001; let var4: number = 0.0001; // 使用 Math.truc() 方法对数字进行向下舍入 console.log("After rounding down the " + var1 + " is " + Math.trunc(var1)); console.log("After rounding down the " + var2 + " is " + Math.trunc(var2)); console.log("After rounding down the " + var3 + " is " + Math.trunc(var3)); console.log("After rounding down the " + var4 + " is " + Math.trunc(var4));
编译后,它将生成以下 JavaScript 代码 -
// 具有不同值的数字变量 var var1 = 100; var var2 = 100.9999; var var3 = 100.0001; var var4 = 0.0001; // 使用 Math.truc() 方法对数字进行向下舍入 console.log("After rounding down the " + var1 + " is " + Math.trunc(var1)); console.log("After rounding down the " + var2 + " is " + Math.trunc(var2)); console.log("After rounding down the " + var3 + " is " + Math.trunc(var3)); console.log("After rounding down the " + var4 + " is " + Math.trunc(var4));
输出
上述代码将产生以下输出 -
After rounding down the 100 is 100 After rounding down the 100.9999 is 100 After rounding down the 100.0001 is 100 After rounding down the 0.0001 is 0
使用 Math.ceil() 方法对数字进行向上舍入
用户可以按照以下语法使用 Math.ceil() 方法对数字进行向上舍入。
语法
let num1: number = 99.99; Math.ceil(num1)
这里 num1 是我们希望使用 Math.ceil() 方法对数字进行向上舍入的值。
示例
用户可以看到我们使用了 Math.ceil() 方法对数字进行了向上舍入。在输出中,我们可以观察到 num1 和 num2 变量被向上舍入到 100,尽管 num2 非常接近 99。由于 num3 本身就是整数,所以它保持不变。
// 定义各种数字 let num1: number = 99.99; // 接近 100 的数字 let num2: number = 99.000001; // 接近 99 的数字 let num3: number = 99; // 整数值 let num4: number = -Infinity; // 无穷大值 // 使用 Math.ceil() 方法对数字进行向上舍入 console.log("After rounding up the " + num1 + " is " + Math.ceil(num1)); console.log("After rounding up the " + num2 + " is " + Math.ceil(num2)); console.log("After rounding up the " + num3 + " is " + Math.ceil(num3)); console.log("After rounding up the " + num4 + " is " + Math.ceil(num4));
编译后,它将生成以下 JavaScript 代码 -
// 定义各种数字 var num1 = 99.99; // 接近 100 的数字 var num2 = 99.000001; // 接近 99 的数字 var num3 = 99; // 整数值 var num4 = -Infinity; // 无穷大值 // 使用 Math.ceil() 方法对数字进行向上舍入 console.log("After rounding up the " + num1 + " is " + Math.ceil(num1)); console.log("After rounding up the " + num2 + " is " + Math.ceil(num2)); console.log("After rounding up the " + num3 + " is " + Math.ceil(num3)); console.log("After rounding up the " + num4 + " is " + Math.ceil(num4));
输出
上述代码将产生以下输出 -
After rounding up the 99.99 is 100 After rounding up the 99.000001 is 100 After rounding up the 99 is 99 After rounding up the -Infinity is -Infinity
使用 toFixed() 方法将数字四舍五入到特定的小数点
在 TypeScript 中,我们可以使用 toFixed() 方法将数字四舍五入到特定的小数点。例如,如果我们想将数字四舍五入到小数点后五位,toFixed() 方法会保留小数点后五位整数。如果小数点后没有五位或超过五位整数,则会在后面补零,但会返回保留五位小数的数字。
语法
let variableA: number = 765.656566565565; variableA.toFixed(decimal_place)
以上语法展示了如何使用 toFixed() 方法对小数进行四舍五入。
参数
decimal_place − 表示要对引用数字进行四舍五入后的小数位数。
示例
在下面的示例中,我们利用 toFixed() 方法将不同的数值四舍五入到特定的小数位。在输出中,我们可以看到变量B 是如何通过添加零将小数位四舍五入到 5 位小数的。
// 具有不同小数位数的不同变量 let variableA: number = 765.656566565565; let variableB: number = 765.2; let variableC: number = 765; // 使用 toFixed() 方法将数字四舍五入到特定的小数位 console.log( "After rounding the " + variableA + " to 3 decimal place is " + variableA.toFixed(3) ); console.log( "After rounding the " + variableB + " to 3 decimal place is " + variableB.toFixed(5) ); console.log( "After rounding the " + variableC + " to 3 decimal place is " + variableC.toFixed(0) );
编译后,它将生成以下 JavaScript 代码 -
// 不同变量的小数位数不同 var variableA = 765.656566565565; var variableB = 765.2; var variableC = 765; // 使用 toFixed() 方法将数字四舍五入到特定的小数位 console.log("After rounding the " + variableA + " to 3 decimal place is " + variableA.toFixed(3)); console.log("After rounding the " + variableB + " to 3 decimal place is " + variableB.toFixed(5)); console.log("After rounding the " + variableC + " to 3 decimal place is " + variableC.toFixed(0));
输出
上述代码将产生以下输出 -
After rounding the 765.656566565565 to 3 decimal place is 765.657 After rounding the 765.2 to 3 decimal place is 765.20000 After rounding the 765 to 3 decimal place is 765
在本教程中,我们讨论了在 TypeScript 中对数字进行舍入的不同方法。