1

I'm taking a value from the mobile application which I'm getting in string format something like "$000"(which actually $0.00 ) similarly I want to convert all the value into two decimal place say if I get "$279"(which is in application actually $2.79) I don't know the correct approach because further in I have compair this value to some other string. so I want to keep this as String but at the same time I want to put decimal after two place always whatever the number.

I tried to Decimal formatter for money but gave me "object as a number format" exception sends

String accLastFourDigits, getCurrAmt, currAmt;
getCurrAmt = getDriver().findElement(by("overview.current_balance")).getText();
DecimalFormat money = new DecimalFormat("$0.00");
currAmt = money.format(getCurrAmt);
Kaan
  • 5,434
  • 3
  • 19
  • 41
XxANxX
  • 121
  • 8

3 Answers3

1

You could use builtin NumberFormat provided by JAVA to parse different country Currencies as shown below. Also I am dividing the resulting number by 100, so as to satisfy the requirement, that $978 is read as 9.78.

  NumberFormat usFormat = NumberFormat.getCurrencyInstance(Locale.US);
    String currencyValue = "$100";
    try {
        System.out.println(usFormat.parse(currencyValue).intValue()/100);
    }catch(ParseException e) {
        e.printStackTrace();
    }

Here, I am setting the currency to US and then parsing a string with dollar sign.

You could also use the format method of NumberFormat to print the currency value in respective currency formats, as shown below

  NumberFormat usFormat = NumberFormat.getCurrencyInstance(Locale.US);
    String currencyValue = "$100";
    try {
        Number value = usFormat.parse(currencyValue).intValue()/100;
        System.out.println("Number value : " + value);

        System.out.printf("In Currency : "+usFormat.format(value));
    }catch(ParseException e) {
        e.printStackTrace();
    }
Vinoth A
  • 1,099
  • 9
  • 10
0

You have this exception because format method expect number type argument. What you need to do then is to remove all non digits characters from the input string

getCurrAmt = getCurrAmt.replaceAll("[^\\d.]", ""); // please note that replaceAll method has poor performance

and parse it to Integer when calling format method

money.format(Integer.parseInt(getCurrAmt))

As pointed out replaceAll method is not very efficient because it needs to compile Pattern every single time and it's better to use Matcher - you can read about this in this topic:

m.antkowicz
  • 13,268
  • 18
  • 37
  • If the *only* extraneous character is a dollar sign (as is implied by the question), it would be faster to just use `String::replace` to remove that dollar sign. Then no regex is needed. But that is only true if the string contains only digits and dollar signs. If the dollar sign is always the first character, `getCurrAmt.substring(1)` would be even faster still. – David Conrad Sep 03 '19 at 20:24
  • 1
    yup you're right - I'm suggesting such method to remove non-digits because it seems that in mobile app you can have some localization and another currency presentation (let say instead of `$1` you can have `106JPY`), and the second reason is that such operation seems not to be run very often (like milion times per second) so you do not need to have the most efficient solution here. Still if it's requirement that first character will always be `$` it's definitely better to use `substring` – m.antkowicz Sep 03 '19 at 20:32
  • @XxANxX are you sure? Actually I even tried your code and this line was definitely fine - the next one was raising the exception – m.antkowicz Sep 03 '19 at 20:33
  • @m.antkowicz sorry I forgot to change the Integer.parseInt() function ... Its working fine OUTPUT: ------------------------------------ value of currnet amount :000 money : $0.00 ------------------------------------ Thanks It worked but my question is does it always work what if I get a value $098 which is actually ($0.98) ??? – XxANxX Sep 03 '19 at 20:39
  • @XxANxX no it won't - how could it understand is it `0.098` or `0.98` or `09.8` or `098.00`? You need to handle this in different way – m.antkowicz Sep 03 '19 at 20:50
  • @m.antkowicz : I don't get it I thought It will always going change string into two decimal place – XxANxX Sep 03 '19 at 20:58
  • @XxANxX yes but `Integer.parseInt` will remove leading zeroes – m.antkowicz Sep 03 '19 at 20:59
0

How about this?

String inputStr = "$279";
NumberFormat usCurrency = NumberFormat.getCurrencyInstance(Locale.US);
usCurrency.setParseIntegerOnly(true);
long num = (Long)usCurrency.parse(inputStr);
BigDecimal amount = new BigDecimal(num);
amount = amount.scaleByPowerOfTen(-2);
log.info("amount: {}", usCurrency.format(amount));
wilx
  • 17,697
  • 6
  • 59
  • 114