blob: e85cbae9fdd2eac2dd25f34bd5e9278567e008c0 [file] [log] [blame]
Victor Chang73229502020-09-17 13:39:19 +01001// © 2017 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
3
4#include "unicode/utypes.h"
5
6#if !UCONFIG_NO_FORMATTING
7#ifndef __NUMBER_ROUNDINGUTILS_H__
8#define __NUMBER_ROUNDINGUTILS_H__
9
10#include "number_types.h"
Victor Changce4bf3c2021-01-19 16:34:24 +000011#include "string_segment.h"
Victor Chang73229502020-09-17 13:39:19 +010012
13U_NAMESPACE_BEGIN
14namespace number {
15namespace impl {
16namespace roundingutils {
17
18enum Section {
19 SECTION_LOWER_EDGE = -1,
20 SECTION_UPPER_EDGE = -2,
21 SECTION_LOWER = 1,
22 SECTION_MIDPOINT = 2,
23 SECTION_UPPER = 3
24};
25
26/**
27 * Converts a rounding mode and metadata about the quantity being rounded to a boolean determining
28 * whether the value should be rounded toward infinity or toward zero.
29 *
30 * <p>The parameters are of type int because benchmarks on an x86-64 processor against OpenJDK
31 * showed that ints were demonstrably faster than enums in switch statements.
32 *
33 * @param isEven Whether the digit immediately before the rounding magnitude is even.
34 * @param isNegative Whether the quantity is negative.
35 * @param section Whether the part of the quantity to the right of the rounding magnitude is
36 * exactly halfway between two digits, whether it is in the lower part (closer to zero), or
37 * whether it is in the upper part (closer to infinity). See {@link #SECTION_LOWER}, {@link
38 * #SECTION_MIDPOINT}, and {@link #SECTION_UPPER}.
39 * @param roundingMode The integer version of the {@link RoundingMode}, which you can get via
40 * {@link RoundingMode#ordinal}.
41 * @param status Error code, set to U_FORMAT_INEXACT_ERROR if the rounding mode is kRoundUnnecessary.
42 * @return true if the number should be rounded toward zero; false if it should be rounded toward
43 * infinity.
44 */
45inline bool
46getRoundingDirection(bool isEven, bool isNegative, Section section, RoundingMode roundingMode,
47 UErrorCode &status) {
Victor Changce4bf3c2021-01-19 16:34:24 +000048 if (U_FAILURE(status)) {
49 return false;
50 }
Victor Chang73229502020-09-17 13:39:19 +010051 switch (roundingMode) {
52 case RoundingMode::UNUM_ROUND_UP:
53 // round away from zero
54 return false;
55
56 case RoundingMode::UNUM_ROUND_DOWN:
57 // round toward zero
58 return true;
59
60 case RoundingMode::UNUM_ROUND_CEILING:
61 // round toward positive infinity
62 return isNegative;
63
64 case RoundingMode::UNUM_ROUND_FLOOR:
65 // round toward negative infinity
66 return !isNegative;
67
68 case RoundingMode::UNUM_ROUND_HALFUP:
69 switch (section) {
70 case SECTION_MIDPOINT:
71 return false;
72 case SECTION_LOWER:
73 return true;
74 case SECTION_UPPER:
75 return false;
76 default:
77 break;
78 }
79 break;
80
81 case RoundingMode::UNUM_ROUND_HALFDOWN:
82 switch (section) {
83 case SECTION_MIDPOINT:
84 return true;
85 case SECTION_LOWER:
86 return true;
87 case SECTION_UPPER:
88 return false;
89 default:
90 break;
91 }
92 break;
93
94 case RoundingMode::UNUM_ROUND_HALFEVEN:
95 switch (section) {
96 case SECTION_MIDPOINT:
97 return isEven;
98 case SECTION_LOWER:
99 return true;
100 case SECTION_UPPER:
101 return false;
102 default:
103 break;
104 }
105 break;
106
107 default:
108 break;
109 }
110
111 status = U_FORMAT_INEXACT_ERROR;
112 return false;
113}
114
115/**
116 * Gets whether the given rounding mode's rounding boundary is at the midpoint. The rounding
117 * boundary is the point at which a number switches from being rounded down to being rounded up.
118 * For example, with rounding mode HALF_EVEN, HALF_UP, or HALF_DOWN, the rounding boundary is at
119 * the midpoint, and this function would return true. However, for UP, DOWN, CEILING, and FLOOR,
120 * the rounding boundary is at the "edge", and this function would return false.
121 *
122 * @param roundingMode The integer version of the {@link RoundingMode}.
123 * @return true if rounding mode is HALF_EVEN, HALF_UP, or HALF_DOWN; false otherwise.
124 */
125inline bool roundsAtMidpoint(int roundingMode) {
126 switch (roundingMode) {
127 case RoundingMode::UNUM_ROUND_UP:
128 case RoundingMode::UNUM_ROUND_DOWN:
129 case RoundingMode::UNUM_ROUND_CEILING:
130 case RoundingMode::UNUM_ROUND_FLOOR:
131 return false;
132
133 default:
134 return true;
135 }
136}
137
138/**
139 * Computes the number of fraction digits in a double. Used for computing maxFrac for an increment.
140 * Calls into the DoubleToStringConverter library to do so.
141 *
142 * @param singleDigit An output parameter; set to a number if that is the
143 * only digit in the double, or -1 if there is more than one digit.
144 */
145digits_t doubleFractionLength(double input, int8_t* singleDigit);
146
147} // namespace roundingutils
148
149
150/**
151 * Encapsulates a Precision and a RoundingMode and performs rounding on a DecimalQuantity.
152 *
153 * This class does not exist in Java: instead, the base Precision class is used.
154 */
155class RoundingImpl {
156 public:
Victor Changd8aa9d52021-01-05 23:49:57 +0000157 RoundingImpl() = default; // defaults to pass-through rounder
Victor Chang73229502020-09-17 13:39:19 +0100158
159 RoundingImpl(const Precision& precision, UNumberFormatRoundingMode roundingMode,
160 const CurrencyUnit& currency, UErrorCode& status);
161
162 static RoundingImpl passThrough();
163
164 /** Required for ScientificFormatter */
165 bool isSignificantDigits() const;
166
167 /**
168 * Rounding endpoint used by Engineering and Compact notation. Chooses the most appropriate multiplier (magnitude
169 * adjustment), applies the adjustment, rounds, and returns the chosen multiplier.
170 *
171 * <p>
172 * In most cases, this is simple. However, when rounding the number causes it to cross a multiplier boundary, we
173 * need to re-do the rounding. For example, to display 999,999 in Engineering notation with 2 sigfigs, first you
174 * guess the multiplier to be -3. However, then you end up getting 1000E3, which is not the correct output. You then
175 * change your multiplier to be -6, and you get 1.0E6, which is correct.
176 *
177 * @param input The quantity to process.
178 * @param producer Function to call to return a multiplier based on a magnitude.
179 * @return The number of orders of magnitude the input was adjusted by this method.
180 */
181 int32_t
182 chooseMultiplierAndApply(impl::DecimalQuantity &input, const impl::MultiplierProducer &producer,
183 UErrorCode &status);
184
185 void apply(impl::DecimalQuantity &value, UErrorCode &status) const;
186
187 /** Version of {@link #apply} that obeys minInt constraints. Used for scientific notation compatibility mode. */
188 void apply(impl::DecimalQuantity &value, int32_t minInt, UErrorCode status);
189
190 private:
191 Precision fPrecision;
192 UNumberFormatRoundingMode fRoundingMode;
Victor Changd8aa9d52021-01-05 23:49:57 +0000193 bool fPassThrough = true; // default value
Victor Changce4bf3c2021-01-19 16:34:24 +0000194
195 // Permits access to fPrecision.
196 friend class units::UnitsRouter;
197
198 // Permits access to fPrecision.
199 friend class UnitConversionHandler;
Victor Chang73229502020-09-17 13:39:19 +0100200};
201
Victor Changce4bf3c2021-01-19 16:34:24 +0000202/**
203 * Parses Precision-related skeleton strings without knowledge of MacroProps
204 * - see blueprint_helpers::parseIncrementOption().
205 *
206 * Referencing MacroProps means needing to pull in the .o files that have the
207 * destructors for the SymbolsWrapper, Usage, and Scale classes.
208 */
209void parseIncrementOption(const StringSegment &segment, Precision &outPrecision, UErrorCode &status);
Victor Chang73229502020-09-17 13:39:19 +0100210
211} // namespace impl
212} // namespace number
213U_NAMESPACE_END
214
215#endif //__NUMBER_ROUNDINGUTILS_H__
216
217#endif /* #if !UCONFIG_NO_FORMATTING */