XxANxX
XxANxX

Reputation: 121

how to display a string with two decimal places which is carrying currency value in dollar

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);

Upvotes: 1

Views: 239

Answers (3)

wilx
wilx

Reputation: 18268

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));

Upvotes: 0

Vinoth A
Vinoth A

Reputation: 1239

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();
    }

Upvotes: 1

m.antkowicz
m.antkowicz

Reputation: 13581

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:

Upvotes: 0

Related Questions