August 09, 2011

Java/Android Currency (Money) Field Validation

Hi everyone,

Today, I have come across one situation where money(currency) field has to be validated. According to Indian currency, we will consider 2 digits after period(.)

When I want to validate the particular field, I have tried with substring() and indexOf() methods. But I am not getting the expected result. I want to have the output ending with .##, So, I have come to know about NumberFormat class from java.text package.

Here goes sample code with output:
Input: 234 / 234.232 / 234.3
Output: 234.0 / 234.23 / 234.3

NumberFormat nf = new DecimalFormat(".##");
System.out.println("Input: "+234+"Output: "+nf.format(234));

Please send your feedback and queries to psrdotcom@gmail.com

No comments:

Featured Post

Java Introdcution

Please send your review and feedback to psrdotcom@gmail.com