如何在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