0

I want to remove zeroes from my string as decimal places, but I am not able to do that. Also, I want the decimal places gone only if zeros are there else the decimal places will be there.

Example:
1234.00 should become 1234
1234.25 should remain 1234.25

Here is the code I am using to do that but its not working.

String price_normal2 ="1234.00";
if(price_normal2.contains(".00")){
    price_normal2.replace(".00","");
    Log.i("PRICEEEEE",""+price_normal2);
}

Please help me in this.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 3
    `replace` returns a new string, use `price_normal2 = price_normal2.replace(".00","");` – Ravi K Thapliyal Aug 20 '21 at 05:54
  • keep in mind Strings are immutable. that's why you need to do it the way Ravi describes – Stultuske Aug 20 '21 at 06:01
  • Is there a reason you want to format a `String`, representing a `double` or `float`? It is much easier to format the floating point number directly through a [`DecimalFormat`](https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/text/DecimalFormat.html). [This answer](https://stackoverflow.com/a/4184015/4216641) has the necessary format. --- Word of advice: for prices (or anything money-related really), I highly recommend to calculate the price in at least an integral type in the background to avoid all the imprecisions of floating points. – Turing85 Aug 20 '21 at 06:03
  • 1
    Does this answer your question? [How to round a number to n decimal places in Java](https://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java) – Jose A. Matarán Aug 20 '21 at 06:28
  • checkout https://developer.android.com/reference/java/text/DecimalFormat – Javatar Aug 20 '21 at 07:59

4 Answers4

1

String class are immutable, So replace method will not replace the value in same object instead it will return the new string which can you store it into another object or the same object by assigning it.

String price_normal2 ="1234.00";
if(price_normal2.contains(".00")){
    price_normal2 = price_normal2.replace(".00","");
    Log.i("PRICEEEEE",""+price_normal2);
}
Bhavin
  • 577
  • 6
  • 11
  • yes! cause it's expected behaviour of replace. it will replace the given portion with provided portion, for your case you should try to use DecimalFormat class which will be easy way to remove zeros in decimal – Bhavin Aug 20 '21 at 07:27
1

Parse the price to double to ensure the validity and then convert it to integer

public static void main(String[] args) {
    String price_normal2 = "1234.00";
    double priceWithFraction = Double.parseDouble(price_normal2);
    int price = (int) priceWithFraction;
    System.out.println("Price " + price);
}
null
  • 548
  • 3
  • 14
1

check this one it will remove the decimal part if it all are zero or remove the any zero on the left side

NumberFormat numberFormat = DecimalFormat.getNumberInstance();
numberFormat.setMinimumFractionDigits(0);
System.out.println(numberFormat.parse("1234.001"));

is it fine with you that 1234.40 be 1234.4 or you want it to be 1234.40?

0

you can use DecimalFormat class

    DecimalFormat decimalFormat = new DecimalFormat("00.##");
    String formatted = decimalFormat.format(Double.parseDouble("1234.30"));
    String[] splitted = formatted.split("\\.");
    if (splitted.length > 1) {
        String dec = (splitted[1].length() == 1) ? splitted[1] + "0" : splitted[1];
        formatted = splitted[0] + "." + dec;
    }

logs

D/mridx: main: 1234.30
MriDx
  • 61
  • 1
  • 6
  • `12.30` becomes `12.3`. –  Aug 20 '21 at 07:40
  • 1234.001 will be come 1234 also –  Aug 20 '21 at 07:57
  • if you want 12.00 to be 12 but 12.30 to be 12.30 then add and if check updating the answer – MriDx Aug 20 '21 at 08:09
  • @justsomeone for that case change decimal format pattern to new DecimalFormat("00.###"); – MriDx Aug 20 '21 at 08:16
  • @MriDx i am not the one who posting the question using # is good if you want to set the fraction digit to certain amount like only up to 3 or 5 digits but if you want it to be as long as it could be then it would not work –  Aug 20 '21 at 08:25
  • @justsomeone yeah... for 3-5 digits adding # is okay. As the question mentioned only two decimal digit, I addd #. – MriDx Aug 20 '21 at 08:29