Here you can find the source of roundFloat(float value, int afterDecimalPoint)
Parameter | Description |
---|---|
value | the double value |
afterDecimalPoint | the number of digits after the decimal point |
public static float roundFloat(float value, int afterDecimalPoint)
//package com.java2s; /*-// w w w .j av a 2s . c o m * * * Copyright 2015 Skymind,Inc. * * * * 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. * */ public class Main { /** * Rounds a double to the given number of decimal places. * * @param value the double value * @param afterDecimalPoint the number of digits after the decimal point * @return the double rounded to the given precision */ public static /*@pure@*/ float roundFloat(float value, int afterDecimalPoint) { float mask = (float) Math.pow(10, (float) afterDecimalPoint); return (float) (Math.round(value * mask)) / mask; } /** * Rounds a double to the next nearest integer value. The JDK version * of it doesn't work properly. * * @param value the double value * @return the resulting integer value */ public static /*@pure@*/ int round(double value) { return value > 0 ? (int) (value + 0.5) : -(int) (Math.abs(value) + 0.5); } }