-1

I have data like this for price number from my broker:

String Price = "00000001970.00";
String Price1 = "0000000295.00";

This is the code I tried to use:

String Price2 = price.replace ("0", "");

But result was Price2 = 197. and my expectation is Price2=1970.

Can someone tell me how to fix this problem?

Jakov
  • 879
  • 2
  • 17
  • 36
bigger
  • 1
  • 1
  • https://stackoverflow.com/questions/2088037/trim-characters-in-java – Dmitry Bychenko Nov 06 '20 at 09:21
  • Does this answer your question? [Trim characters in Java](https://stackoverflow.com/questions/2088037/trim-characters-in-java) – AcK Nov 06 '20 at 11:15
  • 2
    Does this answer your question? [Java - Trim leading or trailing characters from a string?](https://stackoverflow.com/questions/25691415/java-trim-leading-or-trailing-characters-from-a-string) – go2nirvana Nov 06 '20 at 13:39

6 Answers6

1
String price = "00000001970.00";
// If you don't care about the decimals
System.out.println(price.split("\\.")[0].replaceFirst("^0+(?!$)", ""));
// If you do care about the decimals:
System.out.println(price.replaceFirst("^0+(?!$)", ""));

See How to remove leading zeros from alphanumeric text?

jokarl
  • 1,913
  • 2
  • 24
  • 52
1

You need to use a Regex like this:

yourstring.replaceFirst("^0+(?!$)", "");
0

guy. There was a sea of solutions to resolve your problem. For instance, you can look up like below: ` String price = "00000001970.00";

    Double dv = Double.valueOf(price);

    System.out.println(dv);

    int intValue = dv.intValue();

    System.out.println(intValue);

` And you will get a result like below:

` 1970.0

1970`;

Besides, you can also look up like below:

` String price = "00000001970.00";

 String substring = price.substring(0,price.indexOf("."));

 System.out.println(Integer.valueOf(substring));

`

Allen
  • 88
  • 6
0

I'm personally not a big fan of regexes so I avoid them whenever possible.

Assuming that all your Strings are numeric, you could parse them to BigDecimal and then print that however you want. BigDecimal is prefer over Double if you care about precision.

To achieve what you want you could do something like so:

public static void main(String args[]) {
    String price = "00000001970.00";
    String price1 = "0000000295.0011";
    BigDecimal num = new BigDecimal(price);
    BigDecimal num1 = new BigDecimal(price1);
    DecimalFormat df = new DecimalFormat("#.###");
    System.out.println(df.format(num));
    System.out.println(df.format(num1));
}

Those numbers will be formatted as so:

1970
295.001

To learn more about possible formats read DecimalFormat Javadocs.

Amongalen
  • 3,101
  • 14
  • 20
0

Double#parseDouble

public class Main {
    public static void main(String[] args) {
        String strPrice = "00000001970.00";
        double price = Double.parseDouble(strPrice);
        System.out.println(price);
    }
}

Output:

1970.0
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
-1

The simplest way seems to use method replaceFirst from class org.apache.commons.lang3.RegExUtils (Apache Commons):

String price = RegExUtils.replaceFirst("00000001970.00", "0+(\\d)", "$1");

This will replace a substring of first zeros followed by any digit by this digit.

Krzysztof Tomaszewski
  • 1,033
  • 11
  • 16