如何在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()方法来舍入每个数值。
在输出中,用户可以看到,由于number1的小数部分更接近322而不是323,因此它被向下舍入到322。与number1一样,number2最接近323,因此它被向上舍入到323。此外,number4变量被舍入到无穷大,因为它的值是无穷大。
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) );
编译后,它将生成以下JavaScript代码:
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.trunc()方法来去除TypeScript中数字的小数部分。
语法
用户可以按照以下语法学习如何使用Math.trunc()方法向下舍入数字。
let var1: number = 100; Math.trunc(var1)
这里var1是需要向下舍入的值。
示例
在下面的示例中,我们定义了四个数字并用各种值初始化它们。我们使用Math.trunc()方法向下舍入或去除数字的小数部分。
在输出中,用户可以看到var1、var2和var3都被舍入到100,因为该方法去除了小数部分。
// 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));
编译后,它将生成以下JavaScript代码:
// 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));
编译后,它将生成以下JavaScript代码:
// 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()方法将保留小数点后正好五位整数。如果数字的小数点后没有五位或多于五位整数,它会附加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) );
编译后,它将生成以下JavaScript代码:
// 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中舍入数字的不同方法。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP