number_multiplier.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // © 2018 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. #include "unicode/utypes.h"
  4. #if !UCONFIG_NO_FORMATTING
  5. #ifndef __SOURCE_NUMBER_MULTIPLIER_H__
  6. #define __SOURCE_NUMBER_MULTIPLIER_H__
  7. #include "numparse_types.h"
  8. #include "number_decimfmtprops.h"
  9. U_NAMESPACE_BEGIN namespace number {
  10. namespace impl {
  11. /**
  12. * Wraps a {@link Multiplier} for use in the number formatting pipeline.
  13. */
  14. // Exported as U_I18N_API for tests
  15. class U_I18N_API MultiplierFormatHandler : public MicroPropsGenerator, public UMemory {
  16. public:
  17. MultiplierFormatHandler() = default; // WARNING: Leaves object in an unusable state; call setAndChain()
  18. void setAndChain(const Scale& multiplier, const MicroPropsGenerator* parent);
  19. void processQuantity(DecimalQuantity& quantity, MicroProps& micros,
  20. UErrorCode& status) const U_OVERRIDE;
  21. private:
  22. Scale fMultiplier;
  23. const MicroPropsGenerator *fParent;
  24. };
  25. /** Gets a Scale from a DecimalFormatProperties. In Java, defined in RoundingUtils.java */
  26. static inline Scale scaleFromProperties(const DecimalFormatProperties& properties) {
  27. int32_t magnitudeMultiplier = properties.magnitudeMultiplier + properties.multiplierScale;
  28. int32_t arbitraryMultiplier = properties.multiplier;
  29. if (magnitudeMultiplier != 0 && arbitraryMultiplier != 1) {
  30. return Scale::byDoubleAndPowerOfTen(arbitraryMultiplier, magnitudeMultiplier);
  31. } else if (magnitudeMultiplier != 0) {
  32. return Scale::powerOfTen(magnitudeMultiplier);
  33. } else if (arbitraryMultiplier != 1) {
  34. return Scale::byDouble(arbitraryMultiplier);
  35. } else {
  36. return Scale::none();
  37. }
  38. }
  39. } // namespace impl
  40. } // namespace number
  41. U_NAMESPACE_END
  42. #endif //__SOURCE_NUMBER_MULTIPLIER_H__
  43. #endif /* #if !UCONFIG_NO_FORMATTING */