Java Percentage Format numberToPercent2Scale(Double number)

Here you can find the source of numberToPercent2Scale(Double number)

Description

formats the number {Category} NumberUtil {talendTypes} String {param} double(2.5) number: number to format {example} numberToPercent2Scale(0.12345) result: 12,34%

License

Apache License

Declaration

public static String numberToPercent2Scale(Double number) 

Method Source Code

//package com.java2s;
/**//from  www  .  ja  v  a2s. c om
 * Copyright 2015 Jan Lolling jan.lolling@gmail.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.text.NumberFormat;

import java.util.Locale;

public class Main {
    /**
     * formats the number
     * 
     * {Category} NumberUtil
     * 
     * {talendTypes} String
     * 
     * {param} double(2.5) number: number to format
     * 
     * {example} numberToPercent2Scale(0.12345) result: 12,34%
     * 
     */
    public static String numberToPercent2Scale(Double number) {
        if (number != null) {
            return numberToString(roundScale2(number * 100d)) + "%";
        } else {
            return "";
        }
    }

    /**
     * formats the number
     * 
     * {Category} NumberUtil
     * 
     * {talendTypes} String
     * 
     * {param} double(2.5) number: number to format
     * 
     * {example} numberToString(1234.12345) result: "1.234,12345"
     * 
     */
    public static String numberToString(Number number) {
        if (number != null) {
            NumberFormat nf = NumberFormat.getInstance(Locale.GERMANY);
            return nf.format(number);
        } else {
            return "";
        }
    }

    /**
     * formats the number
     * 
     * {Category} NumberUtil
     * 
     * {talendTypes} String
     * 
     * {param} double(2.5) number: number to format
     * 
     * {example} numberToString(1234) result: 1.234
     * 
     */
    public static String numberToString(Integer number) {
        if (number != null) {
            NumberFormat nf = NumberFormat.getInstance(Locale.GERMANY);
            return nf.format(number);
        } else {
            return "";
        }
    }

    /**
     * formats the number
     * 
     * {Category} NumberUtil
     * 
     * {talendTypes} String
     * 
     * {param} Integer(1234) number: number to format
     * {param} Integer(6) lengthWithLeadingZeros: length with leading zeors
     * 
     * {example} numberToString(1234, 6) result: 001,234
     * 
     */
    public static String numberToString(Integer number,
            int lengthWithLeadingZeros) {
        if (number != null) {
            String s = String.valueOf(number);
            String rawString = s.replace(".", "").replace(",", "");
            if (lengthWithLeadingZeros > rawString.length()) {
                StringBuilder sb = new StringBuilder();
                for (int i = rawString.length(); i < lengthWithLeadingZeros; i++) {
                    sb.append('0');
                }
                sb.append(s);
                return sb.toString();
            } else {
                return s;
            }
        } else {
            return "";
        }
    }

    /**
     * rounds the number to a value with 2 decimal digits
     * 
     * {Category} NumberUtil
     * 
     * {talendTypes} double | Double
     * 
     * {param} double(2.5) number: number to format
     * 
     * {example} numberToString(1234.12345) result: 1234.12
     * 
     */
    public static Double roundScale2(Number number) {
        if (number != null) {
            return Math.round(number.doubleValue() * 100d) / 100d;
        } else {
            return null;
        }
    }
}

Related

  1. getPercentage(int numerator, int denominator)
  2. getPercentage(long duration, long lapso)
  3. getPercentage(long numerator, long denominator)
  4. getPercentageString(double percent)
  5. getRoundPercent(double f)
  6. percent(double number)
  7. percent(double p1, double p2)
  8. percentage(Double v, String postfix)
  9. percentage(float f)