Java convert a negative/positive string number into negative/pozitive double - java

Does anyone know how to convert the string value type -4,5 or 5,4 into a double -4.5 or 5.4?

Just use Double.parseDouble(Locale, String); Woops, I was confused...
You should use java.text.NumberFormat
NumberFormat format = NumberFormat.getInstance(Locale.FRANCE);
Number number = format.parse("-4,5");
double d = number.doubleValue();
For example, in Belgium or France we use a , as decimal separator. That is why it works.

First replace , with . and then use following function -
Double.valueOf("-4.5");
and
Double.valueOf("5.4");

Related

Change Double format - dot to comma

I must change Double format from dot to comma. I try this:
DecimalFormat df = new DecimalFormat("#,00",
DecimalFormatSymbols.getInstance(Locale.GERMANY));
selectedSheet.addCell(new Number(selectedCellColumn,
selectedCellRow,
Double.valueOf(df.format(value)));
but it`s not working. Have you got any ideas how you can change a dot to a comma?
For (i guess) ApachePOI library to set a different CellUnitStyle use this:
CellStyle unitStyle = workbook.createCellStyle();
unitStyle.setDataFormat((short) BuiltinFormats.getBuiltinFormat("#,##0.00"));
cell.setCellValue(value);
cell.setCellStyle(unit);
Following Formats are builtin available:
ApachePOI builtin Formats
new Number() - I assume this takes double as the third argument? In that case, formatting the double value before you pass it in there is impossible, you need to set the format on how the sheet will display numbers in the cell formatting settings.
Or is the problem that you have a string like "5,3" and want to convert it to double? It looks like the varaible value already has a double value in it.
You have a symbol table in the java doc of DecimalFormat:
Symbol Location Localized? Meaning
------------------------------------------
0 Number Yes Digit
# Number Yes Digit, zero shows as absent
. Number Yes Decimal separator or monetary decimal separator
- Number Yes Minus sign
, Number Yes Grouping separator
etc...
You were using the grouping separator , but you wanted to use the decimal separator ., so change your string from #,00 to #.00:
DecimalFormat df = new DecimalFormat("#.00", DecimalFormatSymbols.getInstance(Locale.GERMANY));
String format = df.format(3.23456);
System.out.println(format); // prints 3,23
Code :
double value = 123.12345;
DecimalFormatSymbols decimalFormatSymbols = DecimalFormatSymbols.getInstance(Locale.GERMANY);
decimalFormatSymbols.setDecimalSeparator(',');
DecimalFormat df = new DecimalFormat("#.###", decimalFormatSymbols);
System.out.println(df.format(value));
selectedSheet.addCell(new Number(selectedCellColumn, selectedCellRow, Double.valueOf(df.format(value)));
Output :
123,123

How to convert a string with multiple points to double in android

I have a string "3,350,800" with multiple points I want to convert to double but have error multiple points
String number = "3,350,800"
number = number.replace(",", ".");
double value = Double.parseDouble(number);
Error : java.lang.NumberFormatException: multiple points
The . character is used as a decimal point in English, and you cannot have more than one of those in a number.
It seems like you're using it as a thousands separator though. This is legal in several locales - you just need to use one that allows it, e.g.:
String number = "3.350.800";
NumberFormat format = NumberFormat.getInstance(Locale.GERMAN);
double value = format.parse(number).doubleValue();
Mix of other answers, no reason to change the , for . and then fetch the German local.
String number = "3,350,800";
NumberFormat format = NumberFormat.getInstance();
double value = format.parse(number).doubleValue();
System.out.println(value);
Output:
3350800.0
you need to use something like this :
String number = "3,350,800";
number = number.replaceAll(",", "");
double value = Double.parseDouble(number);
System.out.println(value);
What number are you trying to get?
3.350.800 is what you're trying to parse as a double,
but that's obviously not a number, since there are "multiple points".
If you just wanna get 3,350,800 as your number, simply change this line -
number = number.replace(",", ".");
to this -
number = number.replace(",", "");

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?

Decimal conversion

I am trying to set decimal values,Below is my input string
String rate="1.000000000";
Converting to double:
Double converted=Double.valueOf(rate);
DecimalFormat format=new DecimalFormat("#.########"); //Setting decimal points to 8
System.out.println("ouput"+format.format(rate)); //Giving output as 1.
I dont understand how to do this,Any hints please.
Regards,
Chaitu
Try
DecimalFormat format=new DecimalFormat("#.00000000");
and
System.out.println("ouput"+format.format(converted));
# will not be displayed for 0, use 0 instead:
String rate="1.010000000";
Double converted=Double.valueOf(rate);
DecimalFormat format=new DecimalFormat("0.00000000");
System.out.println("ouput "+format.format(converted));
Firstly, you're passing the string rate to the DecimalFormat.format method. This will fail, you need to pass the converted object in.
When I tested your code with the above changes, I got 1.01 in the output. To format to 8 decimal places, follow Bala Rs comments. i.e. DecimalFormat format = new DecimalFormat("#.00000000");

Conversion from exponential form to decimal in Java

I want to convert exponential to decimal. e.g. 1.234E3 to 1234.
It is not really a conversion, but about how you display the number. You can use NumberFormat to specify how the number should be displayed.
Check the difference:
double number = 100550000.75;
NumberFormat formatter = new DecimalFormat("#0.00");
System.out.println(number);
System.out.println(formatter.format(number));
How about BigDecimal.valueOf(doubleToFormat).toPlainString()
While working with Doubles and Long numbers in Java you will see that most of the value are displayed in Exponential form.
For Example: In following we are multiplying 2.35 with 10000 and the result is printed.
//Division example
Double a = 2.85d / 10000;
System.out.println("1. " + a.doubleValue());
//Multiplication example
a = 2.85d * 100000000;
System.out.println("2. " + a.doubleValue());
Result:
2.85E-4
2.85E8
Thus you can see the result is printed in exponential format. Now you may want to display the result in pure decimal format like: 0.000285 or 285000000. You can do this simply by using class java.math.BigDecimal. In following example we are using BigDecimal.valueOf() to convert the Double value to BigDecimal and than .toPlainString() to convert it into plain decimal string.
import java.math.BigDecimal;
//..
//..
//Division example
Double a = 2.85d / 10000;
System.out.println("1. " + BigDecimal.valueOf(a).toPlainString());
//Multiplication example
a = 2.85d * 100000000;
System.out.println("2. " + BigDecimal.valueOf(a).toPlainString());
Result:
0.000285
285000000
The only disadvantage of the above method is that it generates long strings of number. You may want to restrict the value and round off the number to 5 or 6 decimal point. For this you can use java.text.DecimalFormat class. In following example we are rounding off the number to 4 decimal point and printing the output.
import java.text.DecimalFormat;
//..
//..
Double a = 2.85d / 10000;
DecimalFormat formatter = new DecimalFormat("0.0000");
System.out.println(formatter .format(a));
Result:
0.0003
I have just tried to compress this code with one line, it will print value of 'a' with two decimal places:
new DecimalFormat("0.00").format(BigDecimal.valueOf(a).toPlainString());
Happy Converting :)
you can turn it into a String using
DecimalFormat
the answer by #b.roth is correct only if it is country specific. I used that method and got i18n issue , because the new DecimalFormat("#0.00) takes the decimal seperator of the particular country. For ex if a country uses decimal seperation as "," , then the formatted value will be in 0,00 ( ex.. 1.2e2 will be 120.00 in some places and 120,00 ) in some places due to i18n issue as said here..
the method that i prefer is `(new BigDecimal("1.2e2").toPlainString() )
just add following tag to jspx:-
<f:convertNumber maxFractionDigits="4" minFractionDigits="2" groupingUsed="false"/>
String data = Long.toString((long) 3.42E8);
System.out.println("**************"+data);
try the following
long l;
double d; //It holds the double value.such as 1.234E3
l=Double.valueOf(time_d).longValue();
you get the decimal value in the variable l.
You can do:
BigDecimal
.valueOf(value)
.setScale(decimalLimit, RoundingMode.HALF_UP)
.toPlainString()

Categories