This question already has answers here:
Show padding zeros using DecimalFormat
(8 answers)
Closed 2 years ago.
I'm using DecimalFormat to round my numbers to exactly with one decimal. However, numbers like 20 don't show up as 20.0 and it just shows 20.
So my code is like this:
int myWholeNumber = 20;
DecimalFormat df = new DecimalFormat("#.#");
double toDouble = Double.parseDouble(df.format(myWholeNumber));
System.out.println(toDouble);
but the output differs from the wanted one:
Output
20
Wanted
20.0
Explicitly have a zero in your format string:
import java.text.DecimalFormat;
public class Main {
public static void main(String[] args) {
int myWholeNumber = 20;
DecimalFormat df = new DecimalFormat("#.0");
double myDoubleNumber = Double.valueOf(myWholeNumber);
System.out.println(df.format(myDoubleNumber));
}
}
Output:
20.0
Try it out here.
Related
I want to format a double value to 6 places precision without rounding.
expected value after format to 6 decimal places
20790123833965.960938
I have tried using decimal format
DecimalFormat formatter = new DecimalFormat("#0.000000");
System.out.println(formatter.format(hashValue) );
And i got this
20790123833965.960000
As #Benoit already said in a comment, to keep the full precision of your number, you need a BigDecimal:
BigDecimal hashValue = new BigDecimal("20790123833965.960938");
DecimalFormat formatter = new DecimalFormat("#0.000000");
System.out.println(formatter.format(hashValue));
Output:
20790123833965.960938
Use this code, it will work.
public class JavaFormatter {
public static void main(String args[]) {
BigDecimal hashValue = new BigDecimal("20790123833965.960938");
DecimalFormat formatter = new DecimalFormat("#.######");
System.out.println(formatter.format(hashValue));
}
}
I'm trying to take a string and convert into a currency. For example I would like to take the string 12579500 and convert it to $125,795.00. I am trying to use DecimalFormat("$#,###.00), to convert the string after I turn it into a double, but what I'm winding up with is $12,579,500.00.
How do I set the last 2 numbers at the end of the string to be decimal points?
Here is my code so far.
DecimalFormat df = new DecimalFormat("$#,###.00");
double ticketPriceNum = Double.parseDouble(ticketPrice);
System.out.print(df.format(ticketPriceNum));
This will make sure that your string is reduced by 2 characters
DecimalFormat df = new DecimalFormat("$#,###.00");
double ticketPriceNum = Double.parseDouble(ticketPrice.substring(0, ticketPrice.length()- 2));
System.out.print(df.format(ticketPriceNum));
try this please
public static void main(String[] args) {
DecimalFormat df = new DecimalFormat("$#,###,##.00");
//if last two digits of ticketprice should be decimal points
double ticketPriceNum = Double.parseDouble(ticketPrice/100);
System.out.println(df.format(ticketPriceNum ));
}
This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 6 years ago.
I want to format a Double obtained from an Object so it only displays three digits after the decimal point. Here's the current code:
Three a = Data.get(index);
// Get the y-axis acceleration value
double b = a.getY();
String accelerationOutUnfiltered = Double.toString(b);
Data[0] = accelerationOutUnfiltered;
Note: I am doing this in Android, and when I use String.format("%.3f", y) this doesn't work and it throws me a error in Android Studio. Currently the above code works but it displays 15 digits after the decimal point.
I have tried several forms, but they all have failed. Please help. Thanks :)
You could use DecimalFormatter.
For example:
double b = a.getY();
DecimalFormat formatter = new DecimalFormat("#.###");
formatter.setRoundingMode(RoundingMode.CEILING);
String formattedDouble = formatter.format(b);
Have tried decimal formatter?
You do something like this:
DecimalFormat df = new DecimalFormat("#.#####");
df.format(<your_number>);
This question already has answers here:
Floating Point with 2 Digits after Point
(3 answers)
Closed 8 years ago.
I want to do some calculations on my app the result will mostly have from 5 to 15 digits after the . for example 24.61835496354822 I want to display the result in a TextView and only show 2 digits after . for example 24.61 please help me
double d = 24.61835496354822;
DecimalFormat f = new DecimalFormat("##.00");
System.out.println(f.format(d));
Either use System.out.printf("%.2f", val);` or
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
System.out.println(df.format(decimalNumber));
new DecimalFormat("##.##").format(number);
Try This :
import java.text.*;
class Decimals {
public static void main(String[] args) {
float f = 24.61835496354822f;
DecimalFormat form = new DecimalFormat("0.00");
System.out.println(form.format(f));
}
}
You could use a simple string format like:
String.format("My value is: %.2f", myFpVal));
If you just want the value, you can make your format string contain just the format instruction like:
String.format("%.2f", myFpVal));
I have a variable of type double, I need to print it in upto 3 decimals of precision but it shouldn't have any trailing zeros...
eg. I need
2.5 // not 2.500
2 // not 2.000
1.375 // exactly till 3 decimals
2.12 // not 2.120
I tried using DecimalFormatter, Am i doing it wrong?
DecimalFormat myFormatter = new DecimalFormat("0.000");
myFormatter.setDecimalSeparatorAlwaysShown(false);
Thanks. :)
Try the pattern "0.###" instead of "0.000":
import java.text.DecimalFormat;
public class Main {
public static void main(String[] args) {
DecimalFormat df = new DecimalFormat("0.###");
double[] tests = {2.50, 2.0, 1.3751212, 2.1200};
for(double d : tests) {
System.out.println(df.format(d));
}
}
}
output:
2.5
2
1.375
2.12
Your solution is almost correct, but you should replace zeros '0' in decimal format pattern by hashes "#".
So it should look like this:
DecimalFormat myFormatter = new DecimalFormat("#.###");
And that line is not necesary (as decimalSeparatorAlwaysShown is false by default):
myFormatter.setDecimalSeparatorAlwaysShown(false);
Here is short summary from javadocs:
Symbol Location Localized? Meaning
0 Number Yes Digit
# Number Yes Digit, zero shows as absent
And the link to javadoc: DecimalFormat
Use NumberFormat class.
Example:
double d = 2.5;
NumberFormat n = NumberFormat.getInstance();
n.setMaximumFractionDigits(3);
System.out.println(n.format(d));
Output will be 2.5, not 2.500.