Reputation: 1577
Input can be given both way in my software, when I give 2.5 , it return 2.5. but whem I given 2,5 it give me, 2.0 . This is my code ;
public PrescriptionNotationParser()
{
final NumberFormat numberFormat = NumberFormat.getInstance();
if (numberFormat instanceof DecimalFormat)
{
doseValueFormat = (DecimalFormat) numberFormat;
doseValueFormat.applyPattern("##.########");
decimalFormatSymbols = doseValueFormat.getDecimalFormatSymbols();
}
}
private double parse(final String valueStr)
{
try
{
return doseValueFormat.parse(valueStr).doubleValue();
}
catch (ParseException e)
{
return 0;
}
}
Expect some expertise help to resolve this issue ?
Upvotes: 0
Views: 134
Reputation: 1066
If you want to display your input always in english format (with a point . ) You can use the placeholder %s-String for any primitive type.
float x = 3.15f, y = 1.2345f;
System.out.printf("%.4s and %.5s", x, y);
Output: 3.15 and 1.234
%s is always english formatting regardless of localization.
If you want a specif local formatting, you could also do:
import java.util.Locale;
float x = 3.15f, y = 1.2345f;
System.out.printf(Locale.GERMAN, "%.2f and %.4f", x, y);
Output: 3,15 and 1,2345
And of course you can use an if condition like if Locale.GERMAN then display Locale.GERMAN
Upvotes: 1
Reputation: 96
2.5
is a float, but 2,5
isn't float format, so 2,5
will only get the previous value 2
.
If you want support 2,5
to 2.5
private double parse(String valueStr)//remove final
{
try
{
//replace valueStr "," to "."
return doseValueFormat.parse(valueStr.replace(",", ".")).doubleValue();
}
catch (ParseException e)
{
return 0;
}
}
If you want 23,000 .50
to 23000.5
, only remove ,
and space.
a float .
only one at most.
private double parse(String valueStr)//remove final
{
try
{
//valueStr remove all "," and " "
return doseValueFormat.parse(valueStr.replaceAll(",| ", "")).doubleValue();
}
catch (ParseException e)
{
return 0;
}
}
Upvotes: 2