Java - Formatting float Numbers

Description

Formatting float Numbers

Demo

import java.text.DecimalFormat;
public class Main {

  public static void main(String[] args) {
    int first = 123;
    String hex = Integer.toHexString(first);
    String binary = Integer.toBinaryString(first);
    float second = 123.345F;
    String sec = new DecimalFormat("#0.00").format(second);
    float third = 123.123123F;
    String th = new DecimalFormat("#0.##").format(third);

    System.out.println(String.format("|%1$-" + 10 + "s|", hex) + 
                       String.format("%10s", binary).replace(' ', '0') + "|"+ 
                       String.format("%1$" + 10 + "s", sec) + "|" + 
                       String.format("%1$-" + 10 + "s", th) + "|");
  }/*from   ww w  .  j a  v  a 2s  .c o  m*/

}

Related Topic