Java Second to Minute toDecimal(long seconds, int nanoseconds)

Here you can find the source of toDecimal(long seconds, int nanoseconds)

Description

to Decimal

License

Apache License

Declaration

public static String toDecimal(long seconds, int nanoseconds) 

Method Source Code

//package com.java2s;
/*//from w  ww .j  a v a  2 s .  c  om
 * Copyright 2013 FasterXML.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.
 */

public class Main {
    public static String toDecimal(long seconds, int nanoseconds) {
        StringBuilder sb = new StringBuilder(20).append(seconds)
                .append('.');
        // 14-Mar-2016, tatu: Although we do not yet (with 2.7) trim trailing zeroes,
        //   for general case, 
        if (nanoseconds == 0L) {
            // !!! TODO: 14-Mar-2016, tatu: as per [datatype-jsr310], should trim
            //     trailing zeroes
            if (seconds == 0L) {
                return "0.0";
            }

            //            sb.append('0');
            sb.append("000000000");
        } else {
            StringBuilder nanoSB = new StringBuilder(9);
            nanoSB.append(nanoseconds);
            // May need to both prepend leading nanos (if value less than 0.1)
            final int nanosLen = nanoSB.length();
            int prepZeroes = 9 - nanosLen;
            while (prepZeroes > 0) {
                --prepZeroes;
                sb.append('0');
            }

            // !!! TODO: 14-Mar-2016, tatu: as per [datatype-jsr310], should trim
            //     trailing zeroes
            /*
            // AND possibly trim trailing ones
            int i = nanosLen;
            while ((i > 1) && nanoSB.charAt(i-1) == '0') {
                --i;
            }
            if (i < nanosLen) {
                nanoSB.setLength(i);
            }
             */
            sb.append(nanoSB);
        }
        return sb.toString();
    }
}

Related

  1. secondsToMinutes(int seconds)
  2. secondsToMinutes(int seconds)
  3. secondsToMinutes(int time)
  4. timeToMinute(String time)
  5. timeToMinutes(String in)
  6. toDecimal(long seconds, int nanoseconds)
  7. toMinutes(int hour, int minutes)
  8. toMinutes(int s)
  9. toMinutes(long date)