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

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

Related

Whole numbers to doubles with one decimal number in java [duplicate]

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.

Format a double to 6 decimal places precision

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

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.

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