I have a string salePrice
which can have values like 29.90000,91.01000
and i want to get the output like 29.90,91.01
as in with the two digits after decimal points. i am using a string.
Asked
Active
Viewed 1.4k times
2
-
9those are trailing zeros, not leading zeros – Mark Rotteveel Jun 07 '13 at 12:44
-
3Is there anything you have done to try to solve this problem? We will be more willing to answer your question if you tell us what you have tried so far. (Helpful links for asking better questions: [ask], [FAQ]) – tckmn Jun 07 '13 at 12:45
-
2@Raedwald Not a dup; this question wants to keep the last two digits – tckmn Jun 07 '13 at 12:53
-
read this before you continue http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency , and http://download.java.net/jdk7/archive/b123/docs/api/java/text/NumberFormat.html – user902383 Jun 07 '13 at 16:23
6 Answers
15
One of possible solutions
new BigDecimal("29.90000").setScale(2).toString()
Or if you need to round
new BigDecimal("29.90100").setScale(2, RoundingMode.HALF_UP).toString()
Using BigDecimal because converting from String to double can lose precision!
Choose rounding mode that fits your case.

Grzegorz Żur
- 47,257
- 14
- 109
- 105
-
1
-
1Oh, by the way, that's "lose" :p But a `double` indeed has loose precision :p – fge Jun 07 '13 at 12:49
-
1
-
1
8
Try this...
DecimalFormat df2 = new DecimalFormat( "#,###,###,###.##" );
double dd = 100.2397;
double dd2dec = new Double(df2.format(dd)).doubleValue();

MG_Bautista
- 2,593
- 2
- 18
- 33
-
+1, but shouldn't it be `#,###,###,###.##`? Because the number doesn't always end with `0.00` – tckmn Jun 07 '13 at 12:46
-
-
as a matter of fact it should be "0.00" instead to have fixed 2 digits after decimal point – Evgeniy Dorofeev Jun 07 '13 at 13:13
6
int lastIndex = salePrice.indexOf(".") + 2
salePrice = salePrice.substring(0, lastIndex);

Marvin Emil Brach
- 3,984
- 1
- 32
- 62
2
you can use
String.format("%.2f", value);

Suresh Atta
- 120,458
- 37
- 198
- 307
-
@yannishristofakis Thanks for sharing.value is a common term when representing some value.And convering value to string is not a big deal.So lets wait for OP response,weather he is able to get it or not.Good day. – Suresh Atta Jun 07 '13 at 13:05
1
You can use Apache Commons Mathematics Library
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(2);
nf.setMaximumFractionDigits(2);
ComplexFormat cf = new ComplexFormat(nf);
Complex complex = cf.parse("29.90000");

giannis christofakis
- 8,201
- 4
- 54
- 65
0
Here is an old-school way that matches your question (you always want 2 decimal places)
public class LearnTrim
{
public static void main(final String[] arguments)
{
String value1 = "908.0100";
String value2 = "876.1";
String value3 = "101";
String value4 = "75.75";
String value5 = "31.";
System.out.println(value1 + " => " + trimmy(value1));
System.out.println(value2 + " => " + trimmy(value2));
System.out.println(value3 + " => " + trimmy(value3));
System.out.println(value4 + " => " + trimmy(value4));
System.out.println(value5 + " => " + trimmy(value5));
}
private static String trimmy(final String value)
{
int decimalIndex;
String returnValue;
int valueLength = value.length(); // use StringUtils.length() for null safety.
decimalIndex = value.indexOf('.');
if (decimalIndex != -1)
{
if (decimalIndex < (valueLength - 3))
{
returnValue = value.substring(0, valueLength - 2);
}
else if (decimalIndex == (valueLength - 3))
{
returnValue = value;
}
else if (decimalIndex == (valueLength - 2))
{
returnValue = value + "0";
}
else // decimalIndex == valueLength - 1
{
returnValue = value + "00";
}
}
else
{
returnValue = value + ".00";
}
return returnValue;
}
}

DwB
- 37,124
- 11
- 56
- 82