[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 257
  • Last Modified:

Convert 1,000 to 1000

I am writting an EJB that sits on top of a third party software. This EJB has to be Generic

I get a string from the 3-rd party tool in the form of 1,000. I need to convert this back to 1000 (without the comma).

Is there any way of doing this without any Array stuff. Anyone aware of a Jakarta-commons library or somethins that I can use to do this.
Remember this has to be generic.

Thanks in advance
0
cakirfatih
Asked:
cakirfatih
  • 6
  • 3
  • 2
1 Solution
 
CEHJCommented:
s = s.replaceAll(",", "");
0
 
cakirfatihAuthor Commented:
It has to be generic,
If s = "hi, nice to meet you"
The comma will be gone I dont need that.
Thanks
0
 
CEHJCommented:
That is
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
cakirfatihAuthor Commented:
What do you mean ?
0
 
CEHJCommented:
What i posted *is* generic
0
 
bcsonkaCommented:
If the string you are receiving will always be an integer that may or may not contain a comma, you can use the NumberFormat class to convert it such that the comma is removed:

String str = "1,000";
NumberFormat fmt = NumberFormat.getInstance();
Number num = fmt.parse(str);
String newStr = Integer.toString(num.intValue());

newStr would end up being "1000" in this case.

I hope that helps.
0
 
cakirfatihAuthor Commented:
The string I will be receiving may not always be an integer.
Anything I can do to test wether it is or not prior to the conversion to Number
Thanks
0
 
bcsonkaCommented:
If I'm understanding properly, you want to remove the commas in formatted integers but not in text.  If so, then why not try something like this?

public String convertString(String str)
{
      String result = null;

      try
      {
            if(str != null)
            {
                  NumberFormat fmt = NumberFormat.getInstance();
                  Number num = fmt.parse(str);

                  result = Integer.toString(num.intValue());
            }
      }
      catch(ParseException e)
      {
            result = str;
      }

      return result;
}

If you pass "1,000" to this method, it will return "1000".
If you pass "hi, nice to meet you" to this method, it will return "hi, nice to meet you".
If you pass null to this method, it will return null.

I hope that helps.
0
 
CEHJCommented:
Sorry - i got the wrong idea.

>>If you pass "1,000" to this method, it will return "1000".

But if you passed it the String "1,001 dalmations" it would return "1000", not "1000 dalmations". Try:



      public static String convertString2(String s) {
            return s.replaceAll("(?<=\\d),(?=\\d)", "");
      }
0
 
CEHJCommented:
If you need to treat null input:



      public static String convertString2(String s) {
            return s != null? s.replaceAll("(?<=\\d),(?=\\d)", "") : null;
      }
0
 
CEHJCommented:
:-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now