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  
19  import org.hipparchus.util.RyuDouble;
20  
21  /** Formatter used to produce strings from data with high accuracy.
22   * <p>
23   * When producing test output from computed data, we want the shortest
24   * decimal representation of a floating point number that maintains
25   * round-trip safety. That is, a correct parser can recover the exact
26   * original number.
27   * </p>
28   * <p>
29   * For efficiency, this class uses the {@link RyuDouble Ryū} algorithm
30   * for producing shortest string representation with round-trip safety.
31   * </p>
32   * @author Luc Maisonobe
33   * @since 11.0
34   */
35  public class AccurateFormatter implements Formatter {
36  
37      /** Low switch level for exponential format in dates (will never be reached due to {@link #LOW_TRUNCATION}). */
38      private static final int LOW_EXP = -18;
39  
40      /** Truncation level for seconds, to avoid scientific format. */
41      private static final double LOW_TRUNCATION = 1.0e-15;
42  
43      /** Public constructor.
44       */
45      public AccurateFormatter() {
46          // nothing to do
47      }
48  
49      /** Formats to full accuracy.
50       * {@inheritDoc}
51       */
52      @Override
53      public String toString(final double value) {
54          return format(value);
55      }
56  
57      /** Formats the seconds variable with maximum precision needed.
58       * {@inheritDoc}
59       */
60      @Override
61      public String toString(final int year, final int month, final int day,
62                             final int hour, final int minute, final double seconds) {
63          return format(year, month, day, hour, minute, seconds);
64      }
65  
66      /** Format a date.
67       * @param year year
68       * @param month month
69       * @param day day
70       * @param hour hour
71       * @param minute minute
72       * @param seconds seconds
73       * @return date formatted to full accuracy
74       * @deprecated As of 13.0, because static method does not utilize inheritance benefits from {@link Formatter} and
75       * does not check format standards of date time. Use {@link #toString(int, int, int, int, int, double)} instead.
76       */
77      @Deprecated
78      public static String format(final int year, final int month, final int day,
79                                  final int hour, final int minute, final double seconds) {
80          final double truncated = seconds < LOW_TRUNCATION ? 0.0 : seconds;
81          final String s = RyuDouble.doubleToString(truncated, LOW_EXP, RyuDouble.DEFAULT_HIGH_EXP);
82          return String.format(STANDARDIZED_LOCALE, DATE_FORMAT,
83                               year, month, day,
84                               hour, minute, s.charAt(1) == '.' ? "0" + s : s);
85      }
86  
87      /** Format a double number.
88       * @param value number to format
89       * @return number formatted to full accuracy
90       * @deprecated As of 13.0, because Static method does not utilize inheritance benefits from {@link Formatter}.
91       * Use {@link #toString(double)} instead.
92       */
93      @Deprecated
94      public static String format(final double value) {
95          return RyuDouble.doubleToString(value);
96      }
97  }