Printing a double value with 2 decimals with a . not a , [duplicate] - java

This question already has answers here:
How to change the decimal separator of DecimalFormat from comma to dot/point?
(8 answers)
Closed 5 years ago.
I have a little problem in an app I am working on. I am using some double values and I want to place them in some TextViews, but when the number is placed, instead of "." it appears ",". I would like to have a dot so I can import the value with a substring.
DecimalFormat precision = new DecimalFormat("0.00");
final TextView totalPrice = (TextView) getView().findViewById(R.id.actualPrice);
double price = 200.12357123;
totalPrice.setText("$" + precision.format(price));
Inside the TextView will appear 200,12 and I would like to appear 200.12, so I can convert the text in a double value later on.

Try this
Locale currentLocale = Locale.getDefault();
String formatString = "#,###,###,###.##";
DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(currentLocale);
otherSymbols.setDecimalSeparator(',');
otherSymbols.setGroupingSeparator('.');
DecimalFormat df = new DecimalFormat(formatString, otherSymbols);
double price = 200.12357123;
Log.i("TAG", "" + "$ " + df.format(price));
OUTPUT

Use below code it will give an exactly same answer as you want:
double price = 200.12357123;
String s=String.format(Locale.US, "$%.2f", price);

You can change the '.' with ',' ,when you want to convert to double.

Related

How to format a Double coming from a Object in Java? [duplicate]

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>);

how to format string to show two decimal places [duplicate]

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.

Formatting string number into double variable with two number after decimal point

I'm getting from server a string value formatted as follow: 14.5000
I need to create a double variable from it with two number after decimal point: 14.50. I've tried the following:
DecimalFormat df = new DecimalFormat("#,00");
Double priceD = Double.parseDouble((produitParam.item(paramNb).getTextContent()));
String dx = df.format(priceD);
produit.setPrixTtc(Double.valueOf(dx));
And I'm getting 14.5. If I use DecimalFormat("#.00"), it gives me 15...
Someone could help me with that ?
If you want string with precision upto 2 points after decimal you should use
DecimalFormat df = new DecimalFormat("#.00");
you have used "#,00"
',' is used for specifying grouping Separator.
for more information here is the Java Doc of DecimalFormat:
http://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html
you should look this link.There is a lot of answer your question.
I think.The best answer is DecimalFormat df = new DecimalFormat("#0.00");
for you in link
[how to convert double to 2 number after the dot?

Result with 2 digits after . eg:23.65 [duplicate]

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));

Decimal and special formatting in Java [duplicate]

This question already has answers here:
Java - format double value as dollar amount
(5 answers)
Closed 8 years ago.
I would like to convert my final double value
i.e ( 1.0 )
to be printed out like $1.00. How is this possible in Java?
I imported java.lang.*
DecimalFormat formatter = new DecimalFormat("#.00");
Not sure how to get the dollar sign in there. I didn't know if there was any other way to do this aside from going "$" + .....
Use DecimalFormat
double value = 1.0;
DecimalFormat dformat = new DecimalFormat("#.00");
String modifiedVal = dformat.format(value);
System.out.println("$"+modifiedVal);
The NumberFormat class has a pre-defined instance for currency formatting.
double value = 1.0;
NumberFormat nf = NumberFormat.getCurrencyInstance();
System.out.println(nf.format(value));
You can make use of Locale & NumberFormat classes in Java.
NumberFormat formatter = NumberFormat.getCurrencyInstance(new Locale("en","US"));
System.out.println(formatter.format(1.0));
Use String.format:
// Format double with 2 decimal places and add a dollar sign
String price = "$" + String.format("%.2f", myDouble);
To print it directly, use PrintWriter.printf, e.g. on System.out:
System.out.printf("$%.2f", myDouble);

Categories