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);
You can use the setScale() method to control the rounding of a decimal value as shown in the following example:
double value = 123.456789;
BigDecimal bd = new BigDecimal(value).setScale(2, BigDecimal.ROUND_HALF_UP);
The Double class contains two methods that return a representation of a floating point value according to the IEEE 754 floating-point “double format” bit layout.
The first, doubleToLongBits() does not preserve NaN and returns it as 0x7ff8000000000000L.
The second, doubleToRawLongBits() does preserve NaN and returns the representation of the actual NaN value.
The long returned by these methods can then be converted to a byte array.
byte array = longToByteArray(Double.doubleToRawLongBits(doubleValue));