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 */ 7 8 9 #include <unicode/uversion.h> 10 #include <NumberFormat.h> 11 12 #include <AutoDeleter.h> 13 #include <Autolock.h> 14 #include <FormattingConventionsPrivate.h> 15 16 #include <ICUWrapper.h> 17 18 #include <unicode/numfmt.h> 19 20 21 BNumberFormat::BNumberFormat() 22 : BFormat() 23 { 24 } 25 26 27 BNumberFormat::BNumberFormat(const BNumberFormat &other) 28 : BFormat(other) 29 { 30 } 31 32 33 BNumberFormat::~BNumberFormat() 34 { 35 } 36 37 38 // #pragma mark - Formatting 39 40 41 ssize_t 42 BNumberFormat::Format(char* string, size_t maxSize, const double value) const 43 { 44 BString fullString; 45 status_t status = Format(fullString, value); 46 if (status != B_OK) 47 return status; 48 49 return strlcpy(string, fullString.String(), maxSize); 50 } 51 52 53 status_t 54 BNumberFormat::Format(BString& string, const double value) const 55 { 56 UErrorCode err = U_ZERO_ERROR; 57 ObjectDeleter<NumberFormat> numberFormatter(NumberFormat::createInstance( 58 *BFormattingConventions::Private(&fConventions).ICULocale(), 59 UNUM_DECIMAL, err)); 60 61 if (numberFormatter.Get() == NULL) 62 return B_NO_MEMORY; 63 if (U_FAILURE(err)) 64 return B_BAD_VALUE; 65 66 UnicodeString icuString; 67 numberFormatter->format(value, icuString); 68 69 string.Truncate(0); 70 BStringByteSink stringConverter(&string); 71 icuString.toUTF8(stringConverter); 72 73 return B_OK; 74 } 75 76 77 ssize_t 78 BNumberFormat::Format(char* string, size_t maxSize, const int32 value) const 79 { 80 BString fullString; 81 status_t status = Format(fullString, value); 82 if (status != B_OK) 83 return status; 84 85 return strlcpy(string, fullString.String(), maxSize); 86 } 87 88 89 status_t 90 BNumberFormat::Format(BString& string, const int32 value) const 91 { 92 UErrorCode err = U_ZERO_ERROR; 93 ObjectDeleter<NumberFormat> numberFormatter(NumberFormat::createInstance( 94 *BFormattingConventions::Private(&fConventions).ICULocale(), 95 UNUM_DECIMAL, err)); 96 97 if (numberFormatter.Get() == NULL) 98 return B_NO_MEMORY; 99 if (U_FAILURE(err)) 100 return B_BAD_VALUE; 101 102 UnicodeString icuString; 103 numberFormatter->format((int32_t)value, icuString); 104 105 string.Truncate(0); 106 BStringByteSink stringConverter(&string); 107 icuString.toUTF8(stringConverter); 108 109 return B_OK; 110 } 111 112 113 ssize_t 114 BNumberFormat::FormatMonetary(char* string, size_t maxSize, const double value) 115 const 116 { 117 BString fullString; 118 status_t status = FormatMonetary(fullString, value); 119 if (status != B_OK) 120 return status; 121 122 return strlcpy(string, fullString.String(), maxSize); 123 } 124 125 126 status_t 127 BNumberFormat::FormatMonetary(BString& string, const double value) const 128 { 129 if (string == NULL) 130 return B_BAD_VALUE; 131 132 UErrorCode err = U_ZERO_ERROR; 133 ObjectDeleter<NumberFormat> numberFormatter( 134 NumberFormat::createCurrencyInstance( 135 *BFormattingConventions::Private(&fConventions).ICULocale(), 136 err)); 137 138 if (numberFormatter.Get() == NULL) 139 return B_NO_MEMORY; 140 if (U_FAILURE(err)) 141 return B_BAD_VALUE; 142 143 UnicodeString icuString; 144 numberFormatter->format(value, icuString); 145 146 string.Truncate(0); 147 BStringByteSink stringConverter(&string); 148 icuString.toUTF8(stringConverter); 149 150 return B_OK; 151 } 152