Java Number Convert convertSizeG1DetailsToSizeG1(final String size, final char units)

Here you can find the source of convertSizeG1DetailsToSizeG1(final String size, final char units)

Description

Convert SIZE_G1_DETAILS to SIZE_G1.

License

Open Source License

Parameter

Parameter Description
size The size (e.g. '128.0').
units The units (e.g. 'G').

Return

The size block in G1 format (e.g. '131072M').

Declaration

public static String convertSizeG1DetailsToSizeG1(final String size, final char units) 

Method Source Code

//package com.java2s;
/**********************************************************************************************************************
 * garbagecat                                                                                                         *
 *                                                                                                                    *
 * Copyright (c) 2008-2016 Red Hat, Inc.                                                                              *
 *                                                                                                                    * 
 * All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse *
 * Public License v1.0 which accompanies this distribution, and is available at                                       *
 * http://www.eclipse.org/legal/epl-v10.html.                                                                         *
 *                                                                                                                    *
 * Contributors:                                                                                                      *
 *    Red Hat, Inc. - initial API and implementation                                                                  *
 *********************************************************************************************************************/

import java.math.BigDecimal;
import java.math.RoundingMode;

public class Main {
    /**//  w w w .j av a  2  s .com
     * Convert SIZE_G1_DETAILS to SIZE_G1.
     * 
     * @param size
     *            The size (e.g. '128.0').
     * @param units
     *            The units (e.g. 'G').
     * @return The size block in G1 format (e.g. '131072M').
     */
    public static String convertSizeG1DetailsToSizeG1(final String size, final char units) {

        BigDecimal sizeG1 = new BigDecimal(size);
        char unitsG1;

        switch (units) {

        case 'B':
            // Convert to K
            sizeG1 = sizeG1.divide(new BigDecimal("1024"));
            unitsG1 = 'K';
            break;
        case 'K':
            unitsG1 = 'K';
            break;
        case 'M':
            unitsG1 = 'M';
            break;
        case 'G':
            // Convert to M
            sizeG1 = sizeG1.multiply(new BigDecimal("1024"));
            unitsG1 = 'M';
            break;
        default:
            throw new AssertionError("Unexpected units value: " + units);

        }
        sizeG1 = sizeG1.setScale(0, RoundingMode.HALF_EVEN);
        return Integer.toString(sizeG1.intValue()) + unitsG1;
    }
}

Related

  1. convertMinutesToHours(Long minutes)
  2. convertNanoDiff(long startNanos, long stopNanos, TimeUnit timeUnit)
  3. convertNullToOne(final Object value)
  4. convertScientificToStandard(double value)
  5. convertSize(Long size)
  6. convertSizeToMB(long sizeInBytes)
  7. convertsToLong(double v)
  8. convertStringToObjectWrapper(String className, String value)
  9. convertToDecimal(Number n)