Java Franction Convert fraction(StringBuilder buf, int scale, long ms)

Here you can find the source of fraction(StringBuilder buf, int scale, long ms)

Description

fraction

License

Apache License

Declaration

private static void fraction(StringBuilder buf, int scale, long ms) 

Method Source Code

//package com.java2s;
/*/*from   w  w w  .j  a v  a 2 s.  co  m*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to you 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 {
    private static void fraction(StringBuilder buf, int scale, long ms) {
        if (scale > 0) {
            buf.append('.');
            long v1 = scale == 3 ? ms : scale == 2 ? ms / 10
                    : scale == 1 ? ms / 100 : 0;
            number(buf, (int) v1, scale);
        }
    }

    public static StringBuilder number(StringBuilder buf, int v, int n) {
        for (int k = digitCount(v); k < n; k++) {
            buf.append('0');
        }
        return buf.append(v);
    }

    public static int digitCount(int v) {
        for (int n = 1;; n++) {
            v /= 10;
            if (v == 0) {
                return n;
            }
        }
    }
}

Related

  1. fractionAlongRay(double px, double py, double qx, double qy, double rx, double ry)
  2. fractionalPart(float fnum)
  3. fractionalTime(int hour, int minute, int second)
  4. fractionDigits(double number)