AccurateFormatter.java

  1. /* Copyright 2002-2022 CS GROUP
  2.  * Licensed to CS GROUP (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.orekit.utils;

  18. import java.util.Locale;

  19. import org.hipparchus.util.RyuDouble;

  20. /** Formatter used to produce strings from data with high accuracy.
  21.  * <p>
  22.  * When producing test output from computed data, we want the shortest
  23.  * decimal representation of a floating point number that maintains
  24.  * round-trip safety. That is, a correct parser can recover the exact
  25.  * original number.
  26.  * </p>
  27.  * <p>
  28.  * For efficiency, this class uses the {@link RyuDouble RyĆ«} algorithm
  29.  * for producing shortest string representation with round-trip safety.
  30.  * </p>
  31.  * @author Luc Maisonobe
  32.  * @since 11.0
  33.  */
  34. public class AccurateFormatter {

  35.     /**
  36.      * Standardized locale to use, to ensure files can be exchanged without
  37.      * internationalization issues.
  38.      */
  39.     public static final Locale STANDARDIZED_LOCALE = Locale.US;

  40.     /** String format used for dates. **/
  41.     private static final String DATE_FORMAT = "%04d-%02d-%02dT%02d:%02d:%s";

  42.     /** Low switch level for exponential format in dates (will never be reached due to {@link #LOW_TRUNCATION}). */
  43.     private static final int LOW_EXP = -18;

  44.     /** Truncation level for seconds, to avoid scientific format). */
  45.     private static final double LOW_TRUNCATION = 1.0e-15;

  46.     /** Private constructor for a utility class.
  47.      */
  48.     private AccurateFormatter() {
  49.         // nothing to do
  50.     }

  51.     /** Format a double number.
  52.      * @param value number to format
  53.      * @return number formatted to full accuracy
  54.      */
  55.     public static String format(final double value) {
  56.         return RyuDouble.doubleToString(value);
  57.     }

  58.     /** Format a date.
  59.      * @param year year
  60.      * @param month month
  61.      * @param day day
  62.      * @param hour hour
  63.      * @param minute minute
  64.      * @param seconds seconds
  65.      * @return date formatted to full accuracy
  66.      */
  67.     public static String format(final int year, final int month, final int day,
  68.                                 final int hour, final int minute, final double seconds) {
  69.         final double truncated = seconds < LOW_TRUNCATION ? 0.0 : seconds;
  70.         final String s = RyuDouble.doubleToString(truncated, LOW_EXP, RyuDouble.DEFAULT_HIGH_EXP);
  71.         return String.format(STANDARDIZED_LOCALE, DATE_FORMAT,
  72.                              year, month, day,
  73.                              hour, minute, s.charAt(1) == '.' ? "0" + s : s);
  74.     }

  75. }