AccurateFormatter.java

  1. /* Copyright 2002-2025 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 org.hipparchus.util.RyuDouble;

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

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

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

  38.     /** Public constructor.
  39.      */
  40.     public AccurateFormatter() {
  41.         // nothing to do
  42.     }

  43.     /** Formats to full accuracy.
  44.      * {@inheritDoc}
  45.      */
  46.     @Override
  47.     public String toString(final double value) {
  48.         return format(value);
  49.     }

  50.     /** Formats the seconds variable with maximum precision needed.
  51.      * {@inheritDoc}
  52.      */
  53.     @Override
  54.     public String toString(final int year, final int month, final int day,
  55.                            final int hour, final int minute, final double seconds) {
  56.         return format(year, month, day, hour, minute, seconds);
  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.      * @deprecated As of 13.0, because static method does not utilize inheritance benefits from {@link Formatter} and
  67.      * does not check format standards of date time. Use {@link #toString(int, int, int, int, int, double)} instead.
  68.      */
  69.     @Deprecated
  70.     public static String format(final int year, final int month, final int day,
  71.                                 final int hour, final int minute, final double seconds) {
  72.         final double truncated = seconds < LOW_TRUNCATION ? 0.0 : seconds;
  73.         final String s = RyuDouble.doubleToString(truncated, LOW_EXP, RyuDouble.DEFAULT_HIGH_EXP);
  74.         return String.format(STANDARDIZED_LOCALE, DATE_FORMAT,
  75.                              year, month, day,
  76.                              hour, minute, s.charAt(1) == '.' ? "0" + s : s);
  77.     }

  78.     /** Format a double number.
  79.      * @param value number to format
  80.      * @return number formatted to full accuracy
  81.      * @deprecated As of 13.0, because Static method does not utilize inheritance benefits from {@link Formatter}.
  82.      * Use {@link #toString(double)} instead.
  83.      */
  84.     @Deprecated
  85.     public static String format(final double value) {
  86.         return RyuDouble.doubleToString(value);
  87.     }
  88. }