1   /* Copyright 2002-2026 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 RyuDouble.doubleToString(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          final double truncated = seconds < LOW_TRUNCATION ? 0.0 : seconds;
64          final String s = RyuDouble.doubleToString(truncated, LOW_EXP, RyuDouble.DEFAULT_HIGH_EXP);
65          return String.format(STANDARDIZED_LOCALE, DATE_FORMAT,
66                               year, month, day,
67                               hour, minute, s.charAt(1) == '.' ? "0" + s : s);
68      }
69  
70  }