Help in Locale specific number parsing-Internationalize numbers
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   sankar_sukavanam
Posted On:   Tuesday, November 15, 2005 08:16 AM

I am trying to internationalize numbers. Question 1: In the code when I try to parse "1,2,3,4,5.67" in US locale it is not throwing parse exception even though i have invalid position of group separators(,) in this String. How do we handle this? Question 2: Is it mandatory to check if (form instanceof DecimalFormat) when using NumberFormat.getInstance() DOes this Factory method produce any other Format object? If yes How do we handle the else part? Can we use DecimalFormat objects directly? import java.text.NumberFormat; import java.text.DecimalFormat; import java.text.ParseException; import j   More>>



I am trying to internationalize numbers.
Question 1:
In the code when I try to parse "1,2,3,4,5.67" in US locale it is not throwing parse exception
even though i have invalid position of group separators(,) in this String.
How do we handle this?




Question 2:
Is it mandatory to check if (form instanceof DecimalFormat)
when using NumberFormat.getInstance()
DOes this Factory method produce any other Format object?
If yes How do we handle the else part?
Can we use DecimalFormat objects directly?



			
import java.text.NumberFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.util.Locale;

public class testNumberParsing {

public static void main(String[] args) {
String parseStr = "1,2,3,4,5.67";
Locale locale = Locale.US;
try
{
Number parseNumber = parseNumber(locale,parseStr);
System.out.println("Parsed Number: < "+parseNumber+" >");
}
catch(ParseException pe)
{
System.out.println(pe.getLocalizedMessage());
}
}

public static Number parseNumber (Locale clientLocale,
String numberStr )
throws ParseException
{

NumberFormat form = NumberFormat.getInstance(clientLocale);
System.out.println(form.getClass());

if (form instanceof DecimalFormat)
{
DecimalFormat formatter = (DecimalFormat) form;
formatter.setGroupingUsed(true);
System.out.println(" Grouping Size "+ formatter.getGroupingSize());

ParsePosition parsePos = new ParsePosition(0);

Number parsedNumberObj = (Number) formatter.parse(numberStr,parsePos);
if (numberStr.length() != parsePos.getIndex())
{
String parseExceptionMsg = "Parse Error while parsing String: <"+
numberStr +
" > at Character Position "
+parsePos.getIndex();
throw new ParseException(parseExceptionMsg,parsePos.getIndex());
}
return parsedNumberObj;
}
else
{
form.setGroupingUsed(true);

ParsePosition parsePos = new ParsePosition(0);

Number parsedNumberObj = (Number) form.parse(numberStr,parsePos);
if (numberStr.length() != parsePos.getIndex())
{
String parseExceptionMsg = "Parse Error while parsing String: <"+
numberStr +
" > at Character Position "
+parsePos.getIndex();
throw new ParseException(parseExceptionMsg,parsePos.getIndex());
}
return parsedNumberObj;

}
}
}


   <<Less
About | Sitemap | Contact