BPicture: Fix archive constructor.
[haiku.git] / src / kits / locale / NumberFormat.cpp
blobfefd4a36271d35b0f3619b9d56749676856fd8ab
1 /*
2 * Copyright 2003, Axel Dörfler, axeld@pinc-software.de.
3 * Copyright 2010-2011, Oliver Tappe, zooey@hirschkaefer.de.
4 * Copyright 2012, John Scipione, jscipione@gmail.com
5 * All rights reserved. Distributed under the terms of the MIT License.
6 */
9 #include <unicode/uversion.h>
10 #include <NumberFormat.h>
12 #include <AutoDeleter.h>
13 #include <Autolock.h>
14 #include <FormattingConventionsPrivate.h>
16 #include <ICUWrapper.h>
18 #include <unicode/numfmt.h>
21 BNumberFormat::BNumberFormat()
22 : BFormat()
27 BNumberFormat::BNumberFormat(const BNumberFormat &other)
28 : BFormat(other)
33 BNumberFormat::~BNumberFormat()
38 // #pragma mark - Formatting
41 ssize_t
42 BNumberFormat::Format(char* string, size_t maxSize, const double value) const
44 BString fullString;
45 status_t status = Format(fullString, value);
46 if (status != B_OK)
47 return status;
49 return strlcpy(string, fullString.String(), maxSize);
53 status_t
54 BNumberFormat::Format(BString& string, const double value) const
56 UErrorCode err = U_ZERO_ERROR;
57 ObjectDeleter<NumberFormat> numberFormatter(NumberFormat::createInstance(
58 *BFormattingConventions::Private(&fConventions).ICULocale(),
59 UNUM_DECIMAL, err));
61 if (numberFormatter.Get() == NULL)
62 return B_NO_MEMORY;
63 if (U_FAILURE(err))
64 return B_BAD_VALUE;
66 UnicodeString icuString;
67 numberFormatter->format(value, icuString);
69 string.Truncate(0);
70 BStringByteSink stringConverter(&string);
71 icuString.toUTF8(stringConverter);
73 return B_OK;
77 ssize_t
78 BNumberFormat::Format(char* string, size_t maxSize, const int32 value) const
80 BString fullString;
81 status_t status = Format(fullString, value);
82 if (status != B_OK)
83 return status;
85 return strlcpy(string, fullString.String(), maxSize);
89 status_t
90 BNumberFormat::Format(BString& string, const int32 value) const
92 UErrorCode err = U_ZERO_ERROR;
93 ObjectDeleter<NumberFormat> numberFormatter(NumberFormat::createInstance(
94 *BFormattingConventions::Private(&fConventions).ICULocale(),
95 UNUM_DECIMAL, err));
97 if (numberFormatter.Get() == NULL)
98 return B_NO_MEMORY;
99 if (U_FAILURE(err))
100 return B_BAD_VALUE;
102 UnicodeString icuString;
103 numberFormatter->format((int32_t)value, icuString);
105 string.Truncate(0);
106 BStringByteSink stringConverter(&string);
107 icuString.toUTF8(stringConverter);
109 return B_OK;
113 ssize_t
114 BNumberFormat::FormatMonetary(char* string, size_t maxSize, const double value)
115 const
117 BString fullString;
118 status_t status = FormatMonetary(fullString, value);
119 if (status != B_OK)
120 return status;
122 return strlcpy(string, fullString.String(), maxSize);
126 status_t
127 BNumberFormat::FormatMonetary(BString& string, const double value) const
129 if (string == NULL)
130 return B_BAD_VALUE;
132 UErrorCode err = U_ZERO_ERROR;
133 ObjectDeleter<NumberFormat> numberFormatter(
134 NumberFormat::createCurrencyInstance(
135 *BFormattingConventions::Private(&fConventions).ICULocale(),
136 err));
138 if (numberFormatter.Get() == NULL)
139 return B_NO_MEMORY;
140 if (U_FAILURE(err))
141 return B_BAD_VALUE;
143 UnicodeString icuString;
144 numberFormatter->format(value, icuString);
146 string.Truncate(0);
147 BStringByteSink stringConverter(&string);
148 icuString.toUTF8(stringConverter);
150 return B_OK;