| 1 | // © 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 | |
| 8 | #include "unicode/numberformatter.h" |
| 9 | #include "number_types.h" |
| 10 | |
| 11 | using namespace icu; |
| 12 | using namespace icu::number; |
| 13 | using namespace icu::number::impl; |
| 14 | |
| 15 | |
| 16 | ScientificNotation Notation::scientific() { |
| 17 | // NOTE: ISO C++ does not allow C99 designated initializers. |
| 18 | ScientificSettings settings; |
| 19 | settings.fEngineeringInterval = 1; |
| 20 | settings.fRequireMinInt = false; |
| 21 | settings.fMinExponentDigits = 1; |
| 22 | settings.fExponentSignDisplay = UNUM_SIGN_AUTO; |
| 23 | NotationUnion union_; |
| 24 | union_.scientific = settings; |
| 25 | return {NTN_SCIENTIFIC, union_}; |
| 26 | } |
| 27 | |
| 28 | ScientificNotation Notation::engineering() { |
| 29 | ScientificSettings settings; |
| 30 | settings.fEngineeringInterval = 3; |
| 31 | settings.fRequireMinInt = false; |
| 32 | settings.fMinExponentDigits = 1; |
| 33 | settings.fExponentSignDisplay = UNUM_SIGN_AUTO; |
| 34 | NotationUnion union_; |
| 35 | union_.scientific = settings; |
| 36 | return {NTN_SCIENTIFIC, union_}; |
| 37 | } |
| 38 | |
| 39 | ScientificNotation::ScientificNotation(int8_t fEngineeringInterval, bool fRequireMinInt, |
| 40 | impl::digits_t fMinExponentDigits, |
| 41 | UNumberSignDisplay fExponentSignDisplay) { |
| 42 | ScientificSettings settings; |
| 43 | settings.fEngineeringInterval = fEngineeringInterval; |
| 44 | settings.fRequireMinInt = fRequireMinInt; |
| 45 | settings.fMinExponentDigits = fMinExponentDigits; |
| 46 | settings.fExponentSignDisplay = fExponentSignDisplay; |
| 47 | NotationUnion union_; |
| 48 | union_.scientific = settings; |
| 49 | *this = {NTN_SCIENTIFIC, union_}; |
| 50 | } |
| 51 | |
| 52 | Notation Notation::compactShort() { |
| 53 | NotationUnion union_; |
| 54 | union_.compactStyle = CompactStyle::UNUM_SHORT; |
| 55 | return {NTN_COMPACT, union_}; |
| 56 | } |
| 57 | |
| 58 | Notation Notation::compactLong() { |
| 59 | NotationUnion union_; |
| 60 | union_.compactStyle = CompactStyle::UNUM_LONG; |
| 61 | return {NTN_COMPACT, union_}; |
| 62 | } |
| 63 | |
| 64 | Notation Notation::simple() { |
| 65 | return {}; |
| 66 | } |
| 67 | |
| 68 | ScientificNotation |
| 69 | ScientificNotation::withMinExponentDigits(int32_t minExponentDigits) const { |
| 70 | if (minExponentDigits >= 1 && minExponentDigits <= kMaxIntFracSig) { |
| 71 | ScientificSettings settings = fUnion.scientific; |
| 72 | settings.fMinExponentDigits = static_cast<digits_t>(minExponentDigits); |
| 73 | NotationUnion union_ = {settings}; |
| 74 | return {NTN_SCIENTIFIC, union_}; |
| 75 | } else { |
| 76 | return {U_NUMBER_ARG_OUTOFBOUNDS_ERROR}; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | ScientificNotation |
| 81 | ScientificNotation::withExponentSignDisplay(UNumberSignDisplay exponentSignDisplay) const { |
| 82 | ScientificSettings settings = fUnion.scientific; |
| 83 | settings.fExponentSignDisplay = exponentSignDisplay; |
| 84 | NotationUnion union_ = {settings}; |
| 85 | return {NTN_SCIENTIFIC, union_}; |
| 86 | } |
| 87 | |
| 88 | #endif /* #if !UCONFIG_NO_FORMATTING */ |
| 89 | |