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 && !UPRV_INCOMPLETE_CPP11_SUPPORT 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 scientific()16ScientificNotation 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 engineering()28ScientificNotation 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 compactShort()39Notation Notation::compactShort() { 40 NotationUnion union_; 41 union_.compactStyle = CompactStyle::UNUM_SHORT; 42 return {NTN_COMPACT, union_}; 43 } 44 compactLong()45Notation Notation::compactLong() { 46 NotationUnion union_; 47 union_.compactStyle = CompactStyle::UNUM_LONG; 48 return {NTN_COMPACT, union_}; 49 } 50 simple()51Notation Notation::simple() { 52 return {}; 53 } 54 55 ScientificNotation withMinExponentDigits(int32_t minExponentDigits) const56ScientificNotation::withMinExponentDigits(int32_t minExponentDigits) const { 57 if (minExponentDigits >= 0 && minExponentDigits < kMaxIntFracSig) { 58 ScientificSettings settings = fUnion.scientific; 59 settings.fMinExponentDigits = (int8_t) minExponentDigits; 60 NotationUnion union_ = {settings}; 61 return {NTN_SCIENTIFIC, union_}; 62 } else { 63 return {U_NUMBER_DIGIT_WIDTH_OUTOFBOUNDS_ERROR}; 64 } 65 } 66 67 ScientificNotation withExponentSignDisplay(UNumberSignDisplay exponentSignDisplay) const68ScientificNotation::withExponentSignDisplay(UNumberSignDisplay exponentSignDisplay) const { 69 ScientificSettings settings = fUnion.scientific; 70 settings.fExponentSignDisplay = exponentSignDisplay; 71 NotationUnion union_ = {settings}; 72 return {NTN_SCIENTIFIC, union_}; 73 } 74 75 #endif /* #if !UCONFIG_NO_FORMATTING */ 76