TypeScript 对数字进行四舍五入
在本教程中,我们将学习在TypeScript中对数字进行舍入。在TypeScript中,数字数据类型可以包含带有小数部分的数值,有时,我们需要通过四舍五入来去除小数部分。
例如,如果你想在应用程序中显示某个东西的进度,而你得到的是进度的十进制值,但你想只显示整数值,那么就需要对数字进行四舍五入。
在这里,我们将学习3到4种不同的方法来四舍五入。
使用Math.round()方法对数字进行取整
在TypeScript中,Math.round()方法被用来将数字四舍五入到最接近的小数点。它返回四舍五入后的整数值。
语法
let number1: number = 322.3232;
Math.round(number)
在上述语法中,我们创建了数字变量,并使用math.round()方法对数字进行四舍五入。
参数
Math.round()方法需要一个参数,解释如下。
- number – 这是一个需要四舍五入到最接近的整数的数字值。
示例
在下面的例子中,我们已经创建了四个包含不同数值的数字变量。之后,我们用Math.round()方法对每一个数字值进行了取整。
在输出结果中,用户可以观察到,由于数字1的小数部分接近322,而不是323,所以它被四舍五入为322。和数字1一样,数字2最接近323,所以它被四舍五入为323。另外,数字4的值是无穷大,所以被四舍五入为无穷大。
let number1: number = 322.3232; // number with decimal part near to zero
let number2: number = 0.0300012; // number between 0 and 1
let number3: number = 322.98878; // number with decimal part near to one
let number4: number = Infinity; // infinity value
// Using the Math.round() method for the different number values
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)
);
var number1 = 322.3232; // number with decimal part near to zero
var number2 = 0.0300012; // number between 0 and 1
var number3 = 322.98878; // number with decimal part near to one
var number4 = Infinity; // infinity value
// Using the Math.round() method for the different number values
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()方法总是对作为参数传递的数字值进行舍入。我们也可以使用Math.truc()方法来去除TypeScript中数字的小数部分。
语法
用户可以按照下面的语法来学习使用Math.trunc()方法来舍弃数字。
let var1: number = 100;
Math.trunc(var1)
这里var1是一个需要被四舍五入的值。
示例
在下面的例子中,我们定义了四个数字,并以各种数值对它们进行了初始化。我们使用Math.trunc()方法将数字四舍五入或去除小数点后的部分。
在输出中,用户可以看到var1、var2和var3都是四舍五入的变量,因为该方法去掉了小数部分。
// number variables with the different values
let var1: number = 100;
let var2: number = 100.9999;
let var3: number = 100.0001;
let var4: number = 0.0001;
// using the Math.truc() method to round down the numbers
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));
// number variables with the different values
var var1 = 100;
var var2 = 100.9999;
var var3 = 100.0001;
var var4 = 0.0001;
// using the Math.truc() method to round down the numbers
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已经是一个整数,它保持不变。
// Defining the various numbers
let num1: number = 99.99; // number near to 100
let num2: number = 99.000001; // number near to 99
let num3: number = 99; // integer value
let num4: number = -Infinity; // infinity value
// Using the Math.ceil() method to round up the numbers
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));
// Defining the various numbers
var num1 = 99.99; // number near to 100
var num2 = 99.000001; // number near to 99
var num3 = 99; // integer value
var num4 = -Infinity; // infinity value
// Using the Math.ceil() method to round up the numbers
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()方法在小数点后正好保留五个整数。如果数字在小数点后没有5个或超过5个整数,它会附加0,但它返回的数字正好是5个小数点。
语法
let variableA: number = 765.656566565565;
variableA.toFixed(decimal_place)
上面的语法向我们展示了使用toFixed()方法对十进制数字进行四舍五入。
参数
- decimal_place – 这是到你想四舍五入引用的数字为止的小数位总数。
示例
在下面的例子中,我们用toFixed()方法将不同的数值四舍五入到某一个小数位。在输出中,我们可以看到变量B是如何通过添加零来四舍五入到小数点后5位的。
// Different variables with different number of decimal place values
let variableA: number = 765.656566565565;
let variableB: number = 765.2;
let variableC: number = 765;
// Using the toFixed() method to round number to particular decimal place
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)
);
// Different variables with different number of decimal place values
var variableA = 765.656566565565;
var variableB = 765.2;
var variableC = 765;
// Using the toFixed() method to round number to particular decimal place
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中四舍五入的不同方法。