Two digits after decimal point in Java (four methods)

Source: Internet
Author: User

Excerpt from Http://irobot.iteye.com/blog/285537Java two decimal places (four methods) one

Long is the length of the type, how to have a decimal, is double bar
Java.text.DecimalFormat df=new Java.text.DecimalFormat ("#.##");
Double d=3.14159;
System.out.println (Df.format (d));

Two

Java.math.BigDecimal
BigDecimal bd = new BigDecimal ("3.14159265");
BD = Bd.setscale (2,BIGDECIMAL.ROUND_HALF_UP);

Three

Class test1{
public static void Main (string[] args) {
DOUBLE ret = convert (3.14159);

SYSTEM.OUT.PRINTLN (ret);
}

Static double convert (double value) {
Long L1 = Math.Round (value*100); Rounded
DOUBLE ret = l1/100.0; Note: Use 100.0 instead of 100
return ret;
}
}

Four

Double d = 13.4324;
d= ((int) (d*100))/100;


Two digits after decimal point in Java (four methods)

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.