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