Several ways to preserve decimal points in Java

Source: Internet
Author: User

method One : The String class comes with the method String.Format ("%.2f", 1.2548); The numbers in "%.2f" decide to keep several
method Two : Format the method

private static final Java.text.DecimalFormat df = new Java.text.DecimalFormat ("0.00%");p rivate static final Java.text.DecimalFormat DF2 = new Java.text.DecimalFormat ("0.00");p ublic static String format (Double data) { Synchronized (DF) {return Df.format (data);}} public static String FORMAT2 (Double data) {synchronized (DF2) {return Df2.format (data);}}

  

method Three : Float a =1.23466643f;
int b = (int) (a*10000);
A = (float) b/10000;
System.out.println (a);
method Four : You can not round up the method of preserving the decimal point is one of the special cases where the acquisition takes into account the number of 0 in place
Double num = 1;
Integer b = (int) (num*100000);
String a =b/100000+ "."
+ (b%100000>=10000?b%100000/10000: ("0"))
+ (b%10000>=1000?b%10000/1000: ("0"))
+ (b%1000>=100?b%1000/100: ("0"))
+ (B%100>=10?B%100/10: ("0" +b%10));
System.out.println (a);

Several ways to preserve decimal points in Java

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.