blob: d8235dc601b559218ad2db70a5ae6fd0e18ca168 [file] [log] [blame]
Fredrik Roubert57b72de2018-06-21 14:29:02 +02001// © 2018 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 __SOURCE_NUMBER_MULTIPLIER_H__
8#define __SOURCE_NUMBER_MULTIPLIER_H__
9
10#include "numparse_types.h"
11#include "number_decimfmtprops.h"
12
13U_NAMESPACE_BEGIN namespace number {
14namespace impl {
15
16
17/**
18 * Wraps a {@link Multiplier} for use in the number formatting pipeline.
19 */
20// Exported as U_I18N_API for tests
21class U_I18N_API MultiplierFormatHandler : public MicroPropsGenerator, public UMemory {
22 public:
23 MultiplierFormatHandler() = default; // WARNING: Leaves object in an unusable state; call setAndChain()
24
25 void setAndChain(const Scale& multiplier, const MicroPropsGenerator* parent);
26
27 void processQuantity(DecimalQuantity& quantity, MicroProps& micros,
28 UErrorCode& status) const U_OVERRIDE;
29
30 private:
Victor Chang4578a1c2018-10-22 04:26:58 +010031 Scale fMultiplier;
32 const MicroPropsGenerator *fParent;
Fredrik Roubert57b72de2018-06-21 14:29:02 +020033};
34
35
36/** Gets a Scale from a DecimalFormatProperties. In Java, defined in RoundingUtils.java */
37static inline Scale scaleFromProperties(const DecimalFormatProperties& properties) {
38 int32_t magnitudeMultiplier = properties.magnitudeMultiplier + properties.multiplierScale;
39 int32_t arbitraryMultiplier = properties.multiplier;
40 if (magnitudeMultiplier != 0 && arbitraryMultiplier != 1) {
41 return Scale::byDoubleAndPowerOfTen(arbitraryMultiplier, magnitudeMultiplier);
42 } else if (magnitudeMultiplier != 0) {
43 return Scale::powerOfTen(magnitudeMultiplier);
44 } else if (arbitraryMultiplier != 1) {
45 return Scale::byDouble(arbitraryMultiplier);
46 } else {
47 return Scale::none();
48 }
49}
50
51
52} // namespace impl
53} // namespace number
54U_NAMESPACE_END
55
56#endif //__SOURCE_NUMBER_MULTIPLIER_H__
57#endif /* #if !UCONFIG_NO_FORMATTING */