i have a double value Such as
Double doubleValue=0.0001;
trying to print this gave me an output as 1.0E-4
So I tried BigDecimal to value of this as.
BigDecimal.valueOf(doubleValue) which ended up giving output as "0.00010".
can anyone let me know how would I get a round up value as "0.0001".( no end trail of 0 after 1)
You can try code similar to following
Double doubleValue=0.0001;
DecimalFormat f = new DecimalFormat("##.0000");
String formattedValue = f.format(doubleValue);
BigDecimal bigDecimalValue = new BigDecimal(formattedValue);
bigDecimalValue.stripTrailingZeros();
Hope this helps
Are you printing with println? You should use printf to format your output.
Double d = 0.000100;
System.out.printf("%.4f",d);
The following will print "0.0001"
Hope this helps!
Related
I have a double which value is set to 0.00. I need the print statement to display it as "$.00". So the whole number part is not displayed
Tried using String.format() method as well as substring method but it did not bring me anywhere...any help is appreciated, thank you!
I can only think of performing two steps. First format the value to two decimal places. Then use String.substring starting from the decimal place. Like,
double x = 9.99;
String withZero = String.format("%.2f", x);
System.out.printf("$%s%n", withZero.substring(withZero.indexOf('.')));
Outputs (as requested)
$.99
Try this:
public static void outputFractional(double value) {
double whole = Math.abs(value);
double fraction = whole - ((long) whole);
DecimalFormat format = new DecimalFormat("$.00");
System.out.println(format.format(fraction));
}
The simples solution is to use DecimalFormat and set the max number of integer digits to 0.
DecimalFormat formatter = new DecimalFormat("$.00");
formatter.setMaximumIntegerDigits(0);
As I understand the question:
Try that one
double amount = 0;
System.out.println("$" + amount);
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?
As the title said:
I tried:
Float.toString(float);
String.valueOf(float);
Float.toHexString(float);
float.toString();
But I found if the Float value = 100.00;
Covert to String value will be 100.0.
How to avoid it? How to be exactly?
Thanks in advance.
Edits-------------
The answers are point to that those which specific the decimal places.
To be exact, you'd better try to format your Float to String using the NumberFormat class hierarchy : http://docs.oracle.com/javase/6/docs/api/java/text/NumberFormat.html
Its "stupid" to keep 2 zeros at the end. All you've to do is add as many zeros as needed at the moment you're printing it, but internally, it's going to be saved as x.0
Example:
printf ("%.2f", 3.14159);
Prints:
3.14
if you are bent upon doing this..
when you get str = "100.0";
String[] strNew = str.split(".");
if(strNew[1].length==1)
{
str=str+"0";
}
BTW A VERY BAD WAY ...
float f = 100.00000f;
String expected = String.format("%.6f", f);
The output of this will be :
100.000000
The length of the numbers after the floating point is done by you.
String.format("%.6f", f) == 100.000000
String.format("%.2f", f) == 100.00
I am trying to read the values from excel sheet using java. When i type more than 10 letters in a cell in excel it is displaying in exponential form like "9.78313E+2". but this is not the real number what i given.
Can any body help me out in this. How can i convert the above exponential form to original number using java language.
Thanks in advance
You can convert as follows,
for example:
new BigDecimal("406770000244E+12").toBigInteger();
Double.parseDouble("9.78313E+2");
gives me
978.313
For more info see the doc.
Following your further queries below, if you've entered 4256411411 and Excel is presenting this as 4.26E+09, putting that value into parseDouble() will only give you 4260000000. If you want the original, perhaps you need to output the Excel file in a fuller format for your Java program, and/or query it using a Java/Excel API (e.g. POI)
Sorry, but none of the answers above Double.parseDouble() and Double.valueOf()... solved my problem, and I continued to get the exponential 'E' value...
This link has a much better approach for the problem, and as I've written there - there is a very good solution:
I needed to convert some double to currency values, and fount that most to the solution are OK but not for me.
The DecimalFormat was eventually the way for me, so here is what I've done:
public String foo(double value) //Got here 6.743240136E7 or something..
{
DecimalFormat formatter;
if(value - (int)value > 0.0)
formatter = new DecimalFormat("0.00"); //Here you can also deal with rounding if you wish..
else
formatter = new DecimalFormat("0");
return formatter.format(value);
}
As you can see, if the number is natural I get - say - 20000000 instead of 2E7 (etc) - without any decimal point.
and if it's decimal, I get only 2 decimal digits.
Hope this will help.
You can use BigDecimal, if you want the exact value that you have in Excel Sheet: -
BigDecimal bd = new BigDecimal("4256411411");
System.out.println(bd.doubleValue());
// If you are sure that's not a floating point number, then use
System.out.println(bd.longValue());
Prints: -
4.256411411E9
4256411411
Try this definitely gona work
double value = 2.06E //real 205809104.13
BigDecimal.valueOf(value)
work for me
Before you read the value from excel sheet format your Column to number.
This may be helps to you
UPDATED
HSSFCell cellE1 = row1.getCell((short) 4);
cellE1.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
Double e1Val = cellE1.getNumericCellValue();
BigDecimal bd = new BigDecimal(e1Val.toString());
long lonVal = bd.longValue();
System.out.println(lonVal);
You can convert easily with the following methods:
Double.valueOf("9.78313E+2").longValue() or
BigDecimal bd = new BigDecimal("9.78313E+2");
long val = bd.longValue();
Assuming that the given number is in a String form.
You can also use wrapper classes :
Double bd=new Double(4445566622);
System.out.println(bd.longValue());
Outputs -4445566622
i had same problem when i only needed String Data that is "1744949451" but it give "1.744949451E9"
so this worked for me
XSSFCell cell = cells.getCell(j);
String value = cell.toString();
if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
//cell.setCellType(XSSFCell.CELL_TYPE_STRING);
value = cell.getRawValue();
}
Log.i("LOG", value + " " + cell.getCellType());
This answer worked for me:
Double bd = new Double(4445566622);
System.out.println(bd.longValue());
// Outputs -4445566622
`Double value = double value ;
Long longValue = value.longValue(); String strCellValue1 = new String(longValue.toString().format("%f",value).replaceAll("\\,?0*$", ""));`
declare a double value and convert to long
convert to string and formated to float the double value finally replace all the value like 123456789,0000 to 123456789
Have to convert the cell into number format before reading the cell value. Below is the code snippet that is used to get the actual value that is in exponential format:
nextCell.setCellType(XSSFCell.CELL_TYPE_NUMERIC);
Double doubleValue = nextCell.getNumericCellValue();
BigDecimal bd = new BigDecimal(doubleValue.toString());
long lonVal = bd.longValue();
String phoneNumber = Long.toString(lonVal).trim();
System.out.print("PhoneNumber " + phoneNumber);
Blog has been wirtten to showcase the actual result.
Regards,
Ankur
Try the following pattern:
Double dblValue = Double.parseDouble("1.99E+07");
String str = String.format("%.2f", dblValue);
System.out.println(str);
Output:
run:
19900000,00
BUILD SUCCESSFUL (total time: 0 seconds)
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");