This question already has answers here:
Best way to Format a Double value to 2 Decimal places [duplicate]
(2 answers)
Closed 6 years ago.
I am facing a slight issue when trying to get two decimal places after pasring double to string and trying to format
pieChart.setCenterText("$" + "" + "" +String.format( "% 1$ .2f", Double.toString(dataCost),""));
can anyone help me improve the above line of code so that it can display to two decimal places? You will also notice that I am trying to leave a space between the dollar sign and the value
You can use String.format("%.2f", d) , your double will be rounded automatically
pieChart.setCenterText("$ " + String.format("%.2f", d));
Following code might help you
double a = 1.234567;
double a = 2;
NumberFormat nf = new DecimalFormat("##.##");
System.out.println(nf.format(a));
System.out.println(nf.format(a));
and the output will be
1.23
2
it only show decimal places if needed, Enjoy! :)
Try Like This
pieChart.setCenterText("$ " + String.format("%.2f", dataCost));
You can use DecimalFormat.
import java.text.DecimalFormat;
DecimalFormat money = new DecimalFormat ("$0.00");
System.out.println(money.format(dataCost));
Try this.
float val = 1245.235645f;
double ans = Double.parseDouble(new DecimalFormat("##.##").format(val));
System.out.println(ans);
Note : ##.## means 2 digits will be displayed after the .(dot)
Package : import java.text.DecimalFormat;
this should helps you.
Related
This question already has answers here:
Use DecimalFormat to get varying amount of decimal places
(4 answers)
Closed 1 year ago.
I was searching for a solution of how to print double with variable length. Means: user will define how many digits he wants after the decimal point, but without success.
I've come to something like, but it doesn't work :
num - double
dec(length) - integer
System.out.printf("%.(%d)f\n", num, dec);
Are you looking for something like the following?
Maybe solution 2 is suitable for you.
Solution 1:
System.out.printf("%.2f", val); // "%.2f" it's a string so you can make it in several ways...eg: "%."+ dec + "f";
Solution 2:
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2); // you can use int variable instead of 2.. eg: df.setMaximumFractionDigits(dec);
System.out.println(df.format(decimalNumber));
This question already has answers here:
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Adding and subtracting doubles are giving strange results [duplicate]
(2 answers)
Closed 3 years ago.
I found many answer speaking about converting String to Double (with two decimals) but I'm facing a weird case. When printing the value no problem, it is right. But when I do calculations the program acts weird.
I have this code:
String str = "21.90";
I want to convert this string into a double. I tried many solution but none works properly.
double amount = Double.valueOf(str);
or
double amount = Double.parseDouble(str);
or
try {
amount = DecimalFormat.getNumberInstance().parse(str).doubleValue();
}
catch (ParseException e){
// error
}
I've tried also with rounding methods like:
double roundOff = Math.round(amount * 100.0) / 100.0;
Number is converted in "21.9" but when I do, for example:
System.out.println(number - 21.8) = 0.09999999999999787
I don't understand why it's doing this.
Thanks in advance for your help.
You are losing precision when you make calculation with double, to make calculation, it better to use BigDecimal, so instead I would go :
String str = "21.90";
BigDecimal result = new BigDecimal(str).subtract(BigDecimal.valueOf(21.8));
System.out.println(result);
=> 0.10
System.out.println is using strings and the double is converted to a string
. A double value has no precsion at all, even if your string has 2 decimals.
So you need to format the converted string:
String str = "21.90";
double amount = Double.parseDouble(str);
System.out.println("double is: " + amount);
double roundOff = Math.round(amount * 100.0) / 100.0;
System.out.println("double rounded is: " + roundOff);
The output is:
double is: 21.9
double rounded is: 21.9
result is: 0,10
Because of my Locale DE a comma is used in the output. Or use:
System.out.println("result is: " + String.format(Locale.US,"%.2f",amount - 21.8));
System.out.println("result is: " + String.format("%.2f",amount - 21.8));
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));
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert a number to 2 decimal places in Java
I need to display a decimal number up to two digits in Java.
For example:
Case1. 2.333 - 2.33
Case2. 3.4 - 3.40
I am able to do the first case. Can anybody help me how to do for the second case.
If you just want to print a double with two digits after the decimal point, use something like this:
double value = 200.3456;
System.out.printf("Value: %.2f", value);
If you want to have the result in a String instead of being printed to the console, use String.format() with the same arguments:
String result = String.format("%.2f", value);
Or use class DecimalFormat:
DecimalFormat df = new DecimalFormat("####0.00");
System.out.println("Value: " + df.format(value));
You can try
System.out.printf("%.2f %.2f%n", 2.333, 3.4);
prints
2.33 3.40