Java and JS take integer summary

Source: Internet
Author: User

Java rounding

  (-) Math class

1.Math.floor : Returns a double value that is less than the maximum integer of the target value.

Example: String v = "13.5"; Math.floor (double.parsedouble (v));

Results:0.

String v = "13.5"; Math.floor (Double.parsedouble (v));

Result:-14.0.

2.Math.ceil : Returns a double value that is greater than the minimum integer of the target value.

Example: String v = "13.5"; Math.ceil (double.parsedouble (v));

Results:0.

String v = "13.5"; Math.ceil (Double.parsedouble (v));

Result:-13.0.

3.Math.rint: Returns a double value, rounded, but even 0.5 takes adjacent even

Example: String v = "13.5"; Math.rint (double.parsedouble (v));

Results:0.

String v = "14.5"; Math.rint (Double.parsedouble (v));//even decimal is 0.5

Results: 14.0.

4.Math.Round: Rounding, but the algorithm is Math.floor (x+0.5), will be the original number plus 0.5 and then rounding down.

Example: String v = "13.5"; Math.Round (double.parsedouble (v));

Results: 14.

String v = "13.5"; Math.Round (Double.parsedouble (v));

Result:-13. -13.6 is-14

 Two DecimalFormat class and NumberFormat class

    Rounded, but even when 0.5 takes an even number adjacent.

Example: String v = "13.5"; New DecimalFormat ("0"). Format (double.parsedouble (v)); or numberformat.getintegerinstance (). Format (double.parsedouble (v));

Results: 14.

String v = "14.5"; Even-numbered decimals are 0.5

Results: 14.

 

 Three BigDecimal Class (recommended)

(1) bigdecimal.round_half_up : Rounding

Example: String v = "13.5"; New BigDecimal (v). Setscale (0, bigdecimal.round_half_up);

Results: 14.

(2) bigdecimal.round_up: Rounding away from zero rounding mode

      Example: String v = "13.5"; New BigDecimal (v). Setscale (0, bigdecimal.round_up);

Results: 14.

String v = "13.5"; New BigDecimal (v). Setscale (0, bigdecimal.round_up);

Result:-14.

(3) bigdecimal.round_down: Rounding mode close to 0

Example: String v = "13.5"; New BigDecimal (v). Setscale (0, Round_down);

Results: 13.

String v = "13.5"; New BigDecimal (v). Setscale (0, Round_down);

Result:-13.

(4) Bigdecimal.round_half_down: Six in five homes

Example: String v = "13.5"; New BigDecimal (v). Setscale (0, Round_half_down);

Results: 13.

String v = "13.6"; New BigDecimal (v). Setscale (0, Round_half_down);

Results: 14.

(5) Bigdecimal.round_floor: The largest integer less than the target value

Example: String v = "13.5"; New BigDecimal (v). Setscale (0, Round_floor);

Results: 13.

String v = "13.5"; New BigDecimal (v). Setscale (0, Round_floor);

Result:-14.

JS rounding

  1.math.ceil: The smallest integer greater than the target value Math.ceil (12.5)------>13,-12.5------>-12

2.math.floor: Maximum integer math.floor (12.5) less than the target value------>12,-12.5------>-13

3.math.round: Rounding Math.Round (12.4)------>12,12.5------>13

4.parseint:parseint (12.5)------>12

      

      

    

Java and JS take integer summary

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.