When you need to format a number as a percent you can use the ‘%’ symbol in your DecimalFormat string. The static helper method getPercentInstance() can also be used if you don’t need complete control over the format string.
When the % symbol is used the value is first multiplied by 100 before applying the format string. So 0.123 would become 12.3%.
NumberFormat format1 = new DecimalFormat("##.####%");
NumberFormat format2 = NumberFormat.getPercentInstance();
String formatted1 = format1.format(value);
String formatted2 = format2.format(value);
written by objects
\\ tags: BigDecimal, DecimalFormat, double, float, format, NumberFormat, percent
Use static method Float.toString()
float f = 123.456f;
String s = Float.toString(f);
// An alternative would be to
// use the following string concatenation
String s2 = "" + f;
written by objects
\\ tags: conversion, float, string
The Float class contains a number of (static) methods for converting to/from various formats. To convert to a float use static method Float.parseFloat().
String s = "123.456";
try
{
float f = Float.parseFloat(s);
}
catch (NumberFormatException ex)
{
// s is not a float
}
written by objects
\\ tags: conversion, float, string