Java Basics Getting Started-float rounding keep two decimal places to note

Source: Internet
Author: User

Float rounding Keep Two decimal places note the place

Recently in Codewars did a very simple topic, but it took me 20 minutes, although the final solution, but in the middle found that the foundation of Java is not really, go back to a good fix

Do not say so much, on the topic: is to write a method, the mileage per gallon conversion into kilometers per liter, keep two digits

At the beginning of the writing is no problem, the problem appears in the retention of two digits there, my last Test code


Import Java.util.regex.pattern;public class Test {/* * Determines whether an integer * *  @param str passed in String *  * @return is an integer returns TRUE, otherwise returns FAL Se */public Static boolean isinteger (String str) {Pattern pattern = pattern.compile ("^[-\\+]?[ \\d]*$ "); return Pattern.matcher (str). matches ();} public static void Main (string[] args) {System.out.println (((Math.Round (2.5555f * 100)/100)); System.out.println (Isinteger ((((Math.Round (2.5555f * 100)/100) + ")"); System.out.println ((float) ((Math.Round (2.5555f * 100)/100)); System.out.println ((float) ((Math.Round (2.5555f * 100f)/100f)); System.out.println (((((Math.Round (2.5555f * 100)/100.0));}}



Output Result:

2
True
2.0
2.56
2.56


I used the second sentence when I was doing the problem.

From the results there can be seen, in fact, is wrong, the need for the sentence should be the third fourth sentence

Here need to point out a more special place, in addition to 100 that place, if only write 100, actually finally is the output of the results have been in the background to be plastic,

Therefore, at the end of the processing divided by 100 here, must after 100 plus F or. 0 means that the final need to convert to float


Last place, I posted a few answers to the topic, including several kinds of float round to keep two decimal places, you can refer to


public class Converter {public  static float mpgtokpm (final float mpg) {    return Math.Round (MPG * 1.609344f/4.546 09188f * 100f)/100f;  }}
Import Java.math.bigdecimal;public class Converter {public  static float mpgtokpm (final float mpg) {    return Bigde Cimal.valueof (MPG * 0.35400604353). Setscale (2, bigdecimal.round_half_up). Floatvalue ();  }}
public class Converter {  private static final float LITER = 4.54609188f;  Private static final float kilometer = 1.609344f;    public static float mpgtokpm (final float mpg) {    return float.parsefloat (String.Format ("%.2f", MPG * kilometer/liter ));  }}
Import Java.math.bigdecimal;public class Converter {public  static float mpgtokpm (final float mpg) {    Double A = (m PG * 1.609344/4.54609188);    BigDecimal bi = new BigDecimal (string.valueof (a));    return (float) Bi.setscale (2, bigdecimal.round_half_up). Doublevalue ();  }}
public class Converter {public  static float mpgtokpm (final float mpg) {    float result = (float) (MPG*1.609344/4. 54609188);    String s = String.Format ("%.2f", result);    return float.parsefloat (s);  }}






Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Basics Getting Started-float rounding keep two decimal places to note

Related Article

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.