Java Number to String numberToString(double num, int digits)

Here you can find the source of numberToString(double num, int digits)

Description

number To String

License

Open Source License

Declaration

public static String numberToString(double num, int digits) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static String numberToString(double num, int digits) {
        if (digits < 0)
            throw new RuntimeException("Digits must be >= 0!");
        String[] parts = String.valueOf(num).split("[.]");
        if (parts[1].matches("0"))
            return parts[0];
        if (parts[1].length() > digits) {
            String end = parts[1].substring(0, digits);
            while (end.endsWith("0"))
                end = end.substring(0, end.length() - 1);
            return parts[0] + '.' + end;
        }/*from   w w  w. ja  va 2s  .c o  m*/
        return parts[0] + '.' + parts[1];
    }
}

Related

  1. numberToString(int gmNumber)
  2. numberToString(int number, int length, String type, int location)
  3. numberToString(Number number)
  4. numberToString(Object instance)