Discover how to get two decimal places in java, include the articles, news, trends, analysis and practical advice about how to get two decimal places in java on alibabacloud.com
This article mainly introduces the Java method to keep the double type two decimal places, please refer to the use of itCopy CodeThe code is as follows:Mport Java.text.DecimalFormat;DecimalFormat df = new DecimalFormat ("##### #0.00");Double D1 = 3.23456Double d2 = 0.0;Double d3 = 2.0;Df.format (D1);Df.format (D2);Df.format (D3);The 3 results were:Copy CodeThe co
Method 1: Use Math.Round to calculate the number format that is returned here.float price=89.89;int itemNum=3;float totalPrice=price*itemNum;float num=(float)(Math.round(totalPrice*100)/100);//如果要求精确4位就*10000然后/10000Method 2: Return with DecimalFormat in string format. The class is fully encapsulated in decimal. Like the% number, thousand bits, decimal precision. Scientific calculation.float price=1.2;Decim
Java float retains two decimal places or decimal placesMethod 1: Use Math.Round to calculate the number format that is returned here.float price=89.89;int itemnum=3;float Totalprice=price*itemnum;float num= (float) (Math.Round (totalprice*100)/100); If the exact 4-bit is required *10000 then/10000Method 2: Return with
1. The stupidest Wayfunction get () { var s = 22.127456 + ""; var str = s.substring (0,s.indexof (".") + 3); alert (str);}2. Regular expression effect is good3. He is more clever ...4.js Reserved 2 decimal places (mandatory)For the number of decimal places greater
JS in the tofixed (n) method to round the number to the number of the specified number of decimal digits, Note: This method can only be used for data type numbers, including Float,int. For example:123.12345.toFixed (2); "123.12" divide the data by using a single statement like this:. Replace (/\d{1,3} (? = (\d{3}) + (\.\d*)? $)/g, ' $, '). Note: thereplace () method can only work on string types , and our team data is usually divided by the number of
Java methods for preserving decimal problems:Method One: Rounding using Java. Math.bigdecimal class Double d = 12.345;BigDecimal bd = new BigDecimal (2,bigdecimal.round_half_up). Doublevalue ();Method Two: Use the Java.text.DecimalFormat class Double d = 12.345;DecimalFormat Dformat = new DecimalFormat (". 00");Dformat.format (d); (". 00") indicates that two
Double num = 123.4567899;
System.out.print (String.Format ("%f%n", num)); 123.456790
System.out.print (String.Format ("%a%n", num));//0X1.EDD3C0BB46929P6
System.out.print ( String.Format ("%g%n", num)); 123.457
Available Identities:
--left-aligned within the minimum width, and cannot be used with 0 identities.
0, if the content length is less than the minimum width, then on the left with 0来 padding.
#, add 0x before adding a 0,16 to the 8 and 16, 8 before the system.
+, the r
follows:
The method of preserving N decimal places in Java, example.
Copy Code code as follows:
Import Java.text.DecimalFormat;
public class Numberfarmat {
public static void Main (string[] args) {
Double sd = 23.2558896635;
The first method, 10000.0, only indicates that the decimal p
Java: Rounding the double value to five, retaining the two decimal places, double rounding to five1. Functions
The double value in the program is accurate to the two digits after the decimal point. It can be rounded up or truncated.
For example, if the input value is 12345.6789, the output value can be 12345.68 or 1234
This article takes the retention of two decimal places as an example. If you have other requirements, you can easily change them.
1. Use Java. Text. decimalformat (recommended)
Java. Text. decimalformat df = new java. Text. decimalformat ("#. 00 ");DF. Format (number );Eg:
Float rounding Keep Two decimal places note the placeRecently 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 fixDo not say so much, on the topic: is to write a method, the mileage per gallon conversion into kilometers per liter, keep
=numberformat.getnumberinstance (); //keep two decimal placesNf.setmaximumfractiondigits (2); //If rounding is not required, you can use the Roundingmode.downNf.setroundingmode (roundingmode.up); returnNf.format (d); } /*** This method is quite simple. * DecimalFormat is a concrete subclass of NumberFormat that formats decimal numbers. * @paramd *@return */ Public StaticString FormatDouble4
in Java, when two integers are divided, because the number after the decimal point is truncated, the result of the operation will be an integer, and if you want to get the result of the operation as a floating point, you must cast both integers one or both to the floating-point numbers. For example: (float) A/b///convert one of the integers to a floating-point
)/100.0;System. out. println ("use Math to perform division:" + d2 );// 2. Use setScale () of BigDecimal to realize rounding and determining the number of decimal places, and convert it into a BigDecimal object.BigDecimal bd = new BigDecimal (d );BigDecimal bd2 = bd. setScale (2, BigDecimal. ROUND_HALF_UP );System. out. println ("obtained through BigDecimal. setScale:" + bd2 );// 3. Retu
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.