Decimal conversion - java

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

Related

Big decimal Conversion with roundup value

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!

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?

Problems with Decimalformat

Hey i am using the following code to format my numbers to have two decimal places
public String format(double num)
{
String temp="";
DecimalFormat df=new DecimalFormat("R.##");
temp=temp+df.format(num);
return temp;
}
When I print out the results it gives me the answeres with only one decimal place.
Please can someone help me stop that.
You use # in DecimalFormat which is the character that says "print the digit. if it is 0 leave it out."
You need to use 0 where 0 is also printed as 0.
E.g.
DecimalFormat df=new DecimalFormat("R.00");
BigDecimal bd = new BigDecimal(123.12331312);
System.out.print(bd.setScale(2,1));
try this
Use following way to format
System.out.printf("%.2f",num);

DecimalFormat not formatting my numbers correctly

I want to round all of my numbers to 1 decimal place.
For example
22.0
-6.1
I am using:
DecimalFormat decimalFormat = new DecimalFormat("0.0");
middlePanelTextView.setText(decimalFormat.format(score.getElevationAngle()));
But whole numbers don't have a 0 appended to it.
-18 should be -18.0 etc.
instead my value shows up as -18.
.# represents a significant digit 0 doesn't.
.# represents and optional digit So try "###.#"
http://developer.android.com/reference/java/text/DecimalFormat.html
I'm not sure about the syntax: but the above article should give you all you need.
Try
middlePanelTextView.setText(""+decimalFormat.format(score.getElevationAngle()));
My TextView was cutting off the digits. My fault. Shrank font size.
Try using
DecimalFormat decimalFormat = new DecimalFormat("#.#");
as the constructor parameter instead.
Try using the following code:
DecimalFormat decimalFormat = new DecimalFormat("0.0#");

Java decimal formatting using String.format?

I need to format a decimal value into a string where I always display at lease 2 decimals and at most 4.
So for example
"34.49596" would be "34.4959"
"49.3" would be "49.30"
Can this be done using the String.format command?
Or is there an easier/better way to do this in Java.
Yes you can do it with String.format:
String result = String.format("%.2f", 10.0 / 3.0);
// result: "3.33"
result = String.format("%.3f", 2.5);
// result: "2.500"
You want java.text.DecimalFormat.
DecimalFormat df = new DecimalFormat("0.00##");
String result = df.format(34.4959);
Here is a small code snippet that does the job:
double a = 34.51234;
NumberFormat df = DecimalFormat.getInstance();
df.setMinimumFractionDigits(2);
df.setMaximumFractionDigits(4);
df.setRoundingMode(RoundingMode.DOWN);
System.out.println(df.format(a));
java.text.NumberFormat is probably what you want.
NumberFormat and DecimalFormat are definitely what you want. Also, note the NumberFormat.setRoundingMode() method. You can use it to control how rounding or truncation is applied during formatting.
You want java.text.DecimalFormat

Categories