How to retain decimal places in Flash

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.