How do I format a number in Java?
How do I format a number in Java?
How do I format a number in Java?
What are the "Best Practices"?
Will I need to round a number before I format it?
32.302342342342343
=>32.30
.7323
=>0.73
etc.
Answer by Espo for How do I format a number in Java?
From this thread, there are different ways to do this:
double r = 5.1234; System.out.println(r); // r is 5.1234 int decimalPlaces = 2; BigDecimal bd = new BigDecimal(r); // setScale is immutable bd = bd.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP); r = bd.doubleValue(); System.out.println(r); // r is 5.12
f = (float) (Math.round(n*100.0f)/100.0f);
DecimalFormat df2 = new DecimalFormat( "#,###,###,##0.00" ); double dd = 100.2397; double dd2dec = new Double(df2.format(dd)).doubleValue(); // The value of dd2dec will be 100.24
The DecimalFormat() seems to be the most dynamic way to do it, and it is also very easy to understand when reading others code.
Answer by Jeff Atwood for How do I format a number in Java?
You and String.format()
will be new best friends!
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html#syntax
String.format("%.2f", (double)value);
Answer by ckpwong for How do I format a number in Java?
Use DecimalFormat.
Answer by Vinko Vrsalovic for How do I format a number in Java?
Round numbers, yes. This is the main example source.
/* * Copyright (c) 1995 - 2008 Sun Microsystems, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of Sun Microsystems nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF
Answer by Tom Hawtin - tackline for How do I format a number in Java?
There are two approaches in the standard library. One is to use java.text.DecimalFormat. The other more cryptic methods (String.format, PrintStream.printf, etc) based around java.util.Formatter should keep C programmers happy(ish).
Answer by Robert J. Walker for How do I format a number in Java?
Be aware that classes that descend from NumberFormat (and most other Format descendants) are not synchronized. It is a common (but dangerous) practice to create format objects and store them in static variables in a util class. In practice, it will pretty much always work until it starts experiencing significant load.
Answer by Stefan Haberl for How do I format a number in Java?
As Robert has pointed out in his answer: DecimalFormat is neither synchronized nor does the API guarantee thread safety (it might depend on the JVM version/vendor you are using).
Use Spring's Numberformatter instead, which is thread safe.
Answer by Nautilus for How do I format a number in Java?
Try this:
String.format("%.2f", 32.302342342342343);
Simple and efficient.
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment