Flash does not have a built-in method for saving decimal places, but when you want to save data such as 3. 14159265354
The following function can solve this problem when two digits are retained.
/**
* The data calculation method retains decimal places
* @ Author litianhua
* @ Num source Value
* @ N: Reserved decimal places
* @ Return retained the data after decimal places
*/
Public static function decimal (Num: Number, N: Number): number {
VaR ts: String = "1 ";
VaR TN: Number = 1;
// Calculate the number of digits to be multiplied
For (VAR I = 0; I <n; I ++ ){
TS + = "0"
}
Tn = Number (TS );
Return math. Round (Num * Tn)/TN
}
This method uses math. round to intercept places. If you change round to floor or Ceil, you can also obtain the upper or lower limit of decimal places. there is also a function that I used long ago before I thought of this method.
/**
* The string cutting method retains decimal places
* @ Author litianhua
* @ Num source Value
* @ N: Reserved decimal places
* @ Return retained the data after decimal places
*/
Public static function decimal (Num: Number, N: Number): number {
// Default Value
If (n = undefined ){
N = 1;
}
VaR numstr: String = string (Num)
VaR numstrarr: array = numstr. Split (".");
// Real number and decimal number
VaR A: String = numstrarr [0];
VaR B: String = numstrarr [1];
// Data check
If (B = undefined ){
// No decimal places
Return number ();
}
B = B. substr (0, N );
Return number (a + "." + B)
}
If this method is used to round the decimal places, it is very troublesome to write a few more lines. Therefore, I am ruthlessly beaten to the cold room.