1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 2015, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 *******************************************************************************
10 #include "unicode/plurrule.h"
11 #include "unicode/unistr.h"
12 #include "unicode/utypes.h"
14 #if !UCONFIG_NO_FORMATTING
16 #include "digitformatter.h"
17 #include "digitgrouping.h"
18 #include "digitinterval.h"
20 #include "precision.h"
21 #include "plurrule_impl.h"
22 #include "smallintformatter.h"
24 #include "valueformatter.h"
25 #include "visibledigits.h"
29 ValueFormatter::~ValueFormatter() {}
31 VisibleDigitsWithExponent
&
32 ValueFormatter::toVisibleDigitsWithExponent(
34 VisibleDigitsWithExponent
&digits
,
35 UErrorCode
&status
) const {
38 return fFixedPrecision
->initVisibleDigitsWithExponent(
39 value
, digits
, status
);
41 case kScientificNotation
:
42 return fScientificPrecision
->initVisibleDigitsWithExponent(
43 value
, digits
, status
);
52 VisibleDigitsWithExponent
&
53 ValueFormatter::toVisibleDigitsWithExponent(
55 VisibleDigitsWithExponent
&digits
,
56 UErrorCode
&status
) const {
59 return fFixedPrecision
->initVisibleDigitsWithExponent(
60 value
, digits
, status
);
62 case kScientificNotation
:
63 return fScientificPrecision
->initVisibleDigitsWithExponent(
64 value
, digits
, status
);
73 static UBool
isNoGrouping(
74 const DigitGrouping
&grouping
,
76 const FixedPrecision
&precision
) {
77 IntDigitCountRange
range(
78 precision
.fMin
.getIntDigitCount(),
79 precision
.fMax
.getIntDigitCount());
80 return grouping
.isNoGrouping(value
, range
);
84 ValueFormatter::isFastFormattable(int32_t value
) const {
88 if (value
== INT32_MIN
) {
94 return fFixedPrecision
->isFastFormattable() && fFixedOptions
->isFastFormattable() && isNoGrouping(*fGrouping
, value
, *fFixedPrecision
);
96 case kScientificNotation
:
106 ValueFormatter::round(DigitList
&value
, UErrorCode
&status
) const {
107 if (value
.isNaN() || value
.isInfinite()) {
112 return fFixedPrecision
->round(value
, 0, status
);
113 case kScientificNotation
:
114 return fScientificPrecision
->round(value
, status
);
123 ValueFormatter::formatInt32(
125 FieldPositionHandler
&handler
,
126 UnicodeString
&appendTo
) const {
130 IntDigitCountRange
range(
131 fFixedPrecision
->fMin
.getIntDigitCount(),
132 fFixedPrecision
->fMax
.getIntDigitCount());
133 return fDigitFormatter
->formatPositiveInt32(
140 case kScientificNotation
:
149 ValueFormatter::format(
150 const VisibleDigitsWithExponent
&value
,
151 FieldPositionHandler
&handler
,
152 UnicodeString
&appendTo
) const {
155 return fDigitFormatter
->format(
162 case kScientificNotation
:
163 return fDigitFormatter
->format(
177 ValueFormatter::countChar32(const VisibleDigitsWithExponent
&value
) const {
180 return fDigitFormatter
->countChar32(
185 case kScientificNotation
:
186 return fDigitFormatter
->countChar32(
188 *fScientificOptions
);
198 ValueFormatter::prepareFixedDecimalFormatting(
199 const DigitFormatter
&formatter
,
200 const DigitGrouping
&grouping
,
201 const FixedPrecision
&precision
,
202 const DigitFormatterOptions
&options
) {
203 fType
= kFixedDecimal
;
204 fDigitFormatter
= &formatter
;
205 fGrouping
= &grouping
;
206 fFixedPrecision
= &precision
;
207 fFixedOptions
= &options
;
211 ValueFormatter::prepareScientificFormatting(
212 const DigitFormatter
&formatter
,
213 const ScientificPrecision
&precision
,
214 const SciFormatterOptions
&options
) {
215 fType
= kScientificNotation
;
216 fDigitFormatter
= &formatter
;
217 fScientificPrecision
= &precision
;
218 fScientificOptions
= &options
;
223 #endif /* !UCONFIG_NO_FORMATTING */