package NumberFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;
import junit.framework.Assert;
import junit.framework.TestCase;
public class numberFormatTest extends TestCase{
public void testNationalFormat(){
double number = 1234.56;
NumberFormat format_us = NumberFormat.getInstance(Locale.US);
Assert.assertEquals(format_us.format(number).toString(), "1,234.56");
NumberFormat format_de = NumberFormat.getInstance(Locale.GERMAN);
Assert.assertEquals(format_de.format(number).toString(), "1.234,56");
}
public void testDestFormat(){
double number = 1234.56;
// if u not care about the nationalization. uses DecimalFormat which is used to binding with one special region...
DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(Locale.US); // can be used as a static final parameter.
Assert.assertEquals(df.format(number), "1,234.56");
}
public void testNationalParse() throws ParseException{
String number_str = "1,234.56"; // same with "1234.56"
double number = 1234.56;
NumberFormat format_us = NumberFormat.getInstance(Locale.US);
double fetched_number = format_us.parse(number_str).doubleValue();
Assert.assertTrue(fetched_number == number);
DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(Locale.US);
Assert.assertTrue(df.parse(number_str).doubleValue() == number);
}
public void testApplyPattern() throws ParseException{
// only two decimal fractions.
String number_str = "1,234.9888"; // same with "1234.9888"
// set the instance to be us
DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(Locale.US);
double number_formated = df.parse(number_str).doubleValue();
Assert.assertTrue( number_formated == 1234.9888);
// apply the format pattern
df.applyPattern("####.##");
Assert.assertTrue( df.parse(df.format(number_formated)).doubleValue() == 1234.99);
}
}
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;
import junit.framework.Assert;
import junit.framework.TestCase;
public class numberFormatTest extends TestCase{
public void testNationalFormat(){
double number = 1234.56;
NumberFormat format_us = NumberFormat.getInstance(Locale.US);
Assert.assertEquals(format_us.format(number).toString(), "1,234.56");
NumberFormat format_de = NumberFormat.getInstance(Locale.GERMAN);
Assert.assertEquals(format_de.format(number).toString(), "1.234,56");
}
public void testDestFormat(){
double number = 1234.56;
// if u not care about the nationalization. uses DecimalFormat which is used to binding with one special region...
DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(Locale.US); // can be used as a static final parameter.
Assert.assertEquals(df.format(number), "1,234.56");
}
public void testNationalParse() throws ParseException{
String number_str = "1,234.56"; // same with "1234.56"
double number = 1234.56;
NumberFormat format_us = NumberFormat.getInstance(Locale.US);
double fetched_number = format_us.parse(number_str).doubleValue();
Assert.assertTrue(fetched_number == number);
DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(Locale.US);
Assert.assertTrue(df.parse(number_str).doubleValue() == number);
}
public void testApplyPattern() throws ParseException{
// only two decimal fractions.
String number_str = "1,234.9888"; // same with "1234.9888"
// set the instance to be us
DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(Locale.US);
double number_formated = df.parse(number_str).doubleValue();
Assert.assertTrue( number_formated == 1234.9888);
// apply the format pattern
df.applyPattern("####.##");
Assert.assertTrue( df.parse(df.format(number_formated)).doubleValue() == 1234.99);
}
}