Set the format for the double - java

How to set the output format lat and lng like this: 0.000000?
double lat = marker.getPosition().latitude;
double lng = marker.getPosition().longitude;
Preferably without converting to a String, so that the output is a double.

No numbers in Java have any kind of output format associated with them. To output them at all, they are converted to a String, even if you call System.out.println(marker.getPosition().latitude).
It is possible to format a double, like any number, but only when converting to a String.
You can use DecimalFormat:
DecimalFormat df = new DecimalFormat("0.000000");
String formattedLat = df.format(marker.getPosition().latitude);
(The 0 is necessary instead of # to make trailing zeroes show up.)
It is also possible to use String.format().
But conversion to a String is necessary if you want to format the number.

You can try this method to round to 6 decimal places using DecimalFormat
double roundDecimals(double d) {
DecimalFormat twoDForm = new DecimalFormat("0.000000");
return Double.valueOf(twoDForm.format(d));
}
use like:
double lat = roundDecimals(marker.getPosition().latitude);
double lng = roundDecimals(marker.getPosition().longitude);
The link posted by #TronicZomB has more relevant information and is a good read.

Related

Parse Double to a specific format

I want to parse Double value to a specific format. I don't want it as a string.
I am trying
DecimalFormat format3DigitsAfterDecimal = new DecimalFormat("#0.000");
double d = 57;
Double.parseDouble(format3DigitsAfterDecimal.format(d));
This returns 57.0, I want 57.000 returned by format method in double.
Am I possibly asking wrong question ?
A double doesn't have a specific format.
Getting a Double value as 57.0 or 57.000 is the same for the computer but not for the user.
As a human, you don't read Double, but String representation of Double.
If you want a Double to be displayed with 3 decimal, you have to turn it to String using DecimalFormat
double d = 57.0;
NumberFormat numFormat = NumberFormat.getInstance();
numFormat.setMaximumFractionDigits(3);
numFormat.setMinimumFractionDigits(3);
System.out.println(numFormat.format(d));
You can't change the value of a decimal, but you can use NumberFormat to display it with three decimal places by setting minimum and maximum fraction digits like in the example above.

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?

How to convert a String number to two three decimal places in Java?

I am trying to convert a String number to two decimal places in Java. I saw lot of posts on satckoverflow but somehow I am getting an exception.
String number = "1.9040409535344458";
String result = String.format("%.2f", number);
System.out.println(result);
This is the exception I am getting -
java.util.IllegalFormatConversionException: f != java.lang.String
I would like to have 1.904 as the output. Does anyone know what wrong I am doing here?
You can try using a NumberFormat. For example:
String number = "1.9040409535344458";
NumberFormat formatter = new DecimalFormat("#0.000");
String result = formatter.format(Double.valueOf(number));
System.out.println(result);
Just declare number to be double :
Double number = 1.9040409535344458;
instead of
String number = "1.9040409535344458";
OUTPUT :
1.90
you should first convert the string into double and then change the decimal value
String number = "1.9040409535344458";
double result = Double.parseDouble(number);//converts the string into double
result = result *100;//adjust the decimal value
System.out.println(result);
You are using a format not meant for a String. I would recommend either converting your String to a double or storing it as a double in the first place. Convert the String to a double, and pass that double to String.format.

Convert string to decimal number with 2 decimal places in Java

In Java, I am trying to parse a string of format "###.##" to a float. The string should always have 2 decimal places.
Even if the String has value 123.00, the float should also be 123.00, not 123.0.
This is what I have so far:
System.out.println("string liters of petrol putting in preferences is " + stringLitersOfPetrol);
Float litersOfPetrol = Float.parseFloat(stringLitersOfPetrol);
DecimalFormat df = new DecimalFormat("0.00");
df.setMaximumFractionDigits(2);
litersOfPetrol = Float.parseFloat(df.format(litersOfPetrol));
System.out.println("liters of petrol before putting in editor: " + litersOfPetrol);
It prints:
string liters of petrol putting in preferences is 010.00
liters of petrol before putting in editor: 10.0
This line is your problem:
litersOfPetrol = Float.parseFloat(df.format(litersOfPetrol));
There you formatted your float to string as you wanted, but but then that string got transformed again to a float, and then what you printed in stdout was your float that got a standard formatting. Take a look at this code
import java.text.DecimalFormat;
String stringLitersOfPetrol = "123.00";
System.out.println("string liters of petrol putting in preferences is "+stringLitersOfPetrol);
Float litersOfPetrol=Float.parseFloat(stringLitersOfPetrol);
DecimalFormat df = new DecimalFormat("0.00");
df.setMaximumFractionDigits(2);
stringLitersOfPetrol = df.format(litersOfPetrol);
System.out.println("liters of petrol before putting in editor : "+stringLitersOfPetrol);
And by the way, when you want to use decimals, forget the existence of double and float as others suggested and just use BigDecimal object, it will save you a lot of headache.
Java convert a String to decimal:
String dennis = "0.00000008880000";
double f = Double.parseDouble(dennis);
System.out.println(f);
System.out.println(String.format("%.7f", f));
System.out.println(String.format("%.9f", new BigDecimal(f)));
System.out.println(String.format("%.35f", new BigDecimal(f)));
System.out.println(String.format("%.2f", new BigDecimal(f)));
This prints:
8.88E-8
0.0000001
0.000000089
0.00000008880000000000000106383001366
0.00
Use BigDecimal:
new BigDecimal(theInputString);
It retains all decimal digits. And you are sure of the exact representation since it uses decimal base, not binary base, to store the precision/scale/etc.
And it is not subject to precision loss like float or double are, unless you explicitly ask it to.
I just want to be sure that the float number will also have 2 decimal places after converting that string.
You can't, because floating point numbers don't have decimal places. They have binary places, which aren't commensurate with decimal places.
If you want decimal places, use a decimal radix.
litersOfPetrol = Float.parseFloat(df.format(litersOfPetrol));
System.out.println("liters of petrol before putting in editor : "+litersOfPetrol);
You print Float here, that has no format at all.
To print formatted float, just use
String formatted = df.format(litersOfPetrol);
System.out.println("liters of petrol before putting in editor : " + formatted);
Float.parseFloat() is the problem as it returns a new float.
Returns a new float initialized to the value represented by the specified String, as performed by the valueOf method of class Float.
You are formatting just for the purpose of display . It doesn't mean the float will be represented by the same format internally .
You can use java.lang.BigDecimal.
I am not sure why are you using parseFloat() twice. If you want to display the float in a certain format then just format it and display it.
Float litersOfPetrol=Float.parseFloat(stringLitersOfPetrol);
DecimalFormat df = new DecimalFormat("0.00");
df.setMaximumFractionDigits(2);
System.out.println("liters of petrol before putting in editor"+df.format(litersOfPetrol));

How to Java String.format with a variable precision?

I'd like to vary the precision of a double representation in a string I'm formatting based on user input. Right now I'm trying something like:
String foo = String.format("%.*f\n", precision, my_double);
however I receive a java.util.UnknownFormatConversionException. My inspiration for this approach was C printf and this resource (section 1.3.1).
Do I have a simple syntax error somewhere, does Java support this case, or is there a better approach?
Edit:
I suppose I could do something like:
String foo = String.format("%." + precision + "f\n", my_double);
but I'd still be interested in native support for such an operation.
You sort of answered your own question - build your format string dynamically... valid format strings follow the conventions outlined here: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html#syntax.
If you want a formatted decimal that occupies 8 total characters (including the decimal point) and you wanted 4 digits after the decimal point, your format string should look like "%8.4f"...
To my knowledge there is no "native support" in Java beyond format strings being flexible.
You can use the DecimalFormat class.
double d1 = 3.14159;
double d2 = 1.235;
DecimalFormat df = new DecimalFormat("#.##");
double roundedD1 = df.format(d); // 3.14
double roundedD2 = df.format(d); // 1.24
If you want to set the precision at run time call:
df.setMaximumFractionDigits(precision)
Why not :
String form = "%."+precision+"f\n";
String foo = String.format(form, my_double);
or :
public static String myFormat(String src, int precision, Object args...)
{
String form = "%."+precision+"f\n";
return String.format(form, args);
}
double pi = Math.PI; // 3.141592653589793
int n = 5;
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(n);
System.out.printf(df.format(pi)); // 3.14159
You can set value of n at runtime. Here from the above code given n = 5 will print 3.14159

Categories