BNumberFormat: add Parse() and GetSymbol().

This commit is contained in:
Adrien Destugues
2017-07-23 11:40:25 +02:00
parent 6cbcfc1911
commit 037ea84ba5
2 changed files with 66 additions and 4 deletions
+6 -4
View File
@@ -10,10 +10,8 @@
enum BNumberElement { enum BNumberElement {
B_NUMBER_ELEMENT_INVALID = B_BAD_DATA, B_DECIMAL_SEPARATOR = 10, // Values 0-9 are reserved for digit symbols
B_NUMBER_ELEMENT_INTEGER = 0, B_GROUPING_SEPARATOR,
B_NUMBER_ELEMENT_FRACTIONAL,
B_NUMBER_ELEMENT_CURRENCY
}; };
class BNumberFormatImpl; class BNumberFormatImpl;
@@ -36,6 +34,10 @@ public:
status_t FormatMonetary(BString& string, status_t FormatMonetary(BString& string,
const double value); const double value);
status_t Parse(const BString& string, double& value);
BString GetSeparator(BNumberElement element);
private: private:
BNumberFormat(const BNumberFormat &other); BNumberFormat(const BNumberFormat &other);
+60
View File
@@ -2,6 +2,7 @@
* Copyright 2003, Axel Dörfler, [email protected]. * Copyright 2003, Axel Dörfler, [email protected].
* Copyright 2010-2011, Oliver Tappe, [email protected]. * Copyright 2010-2011, Oliver Tappe, [email protected].
* Copyright 2012, John Scipione, [email protected] * Copyright 2012, John Scipione, [email protected]
* Copyright 2017, Adrien Destugues, [email protected]
* All rights reserved. Distributed under the terms of the MIT License. * All rights reserved. Distributed under the terms of the MIT License.
*/ */
@@ -15,6 +16,8 @@
#include <ICUWrapper.h> #include <ICUWrapper.h>
#include <unicode/dcfmtsym.h>
#include <unicode/decimfmt.h>
#include <unicode/numfmt.h> #include <unicode/numfmt.h>
@@ -231,3 +234,60 @@ BNumberFormat::FormatMonetary(BString& string, const double value)
return B_OK; return B_OK;
} }
status_t
BNumberFormat::Parse(const BString& string, double& value)
{
NumberFormat* parser = fPrivateData->GetFloat(&fConventions);
if (parser == NULL)
return B_NO_MEMORY;
UnicodeString unicode(string.String());
Formattable result(0);
UErrorCode err = U_ZERO_ERROR;
parser->parse(unicode, result, err);
if (err != U_ZERO_ERROR)
return B_BAD_DATA;
value = result.getDouble();
return B_OK;
}
BString
BNumberFormat::GetSeparator(BNumberElement element)
{
DecimalFormatSymbols::ENumberFormatSymbol symbol;
BString result;
switch(element) {
case B_DECIMAL_SEPARATOR:
symbol = DecimalFormatSymbols::kDecimalSeparatorSymbol;
break;
case B_GROUPING_SEPARATOR:
symbol = DecimalFormatSymbols::kGroupingSeparatorSymbol;
break;
default:
return result;
}
NumberFormat* format = fPrivateData->GetFloat(&fConventions);
DecimalFormat* decimal = dynamic_cast<DecimalFormat*>(format);
if (decimal == NULL)
return result;
const DecimalFormatSymbols* symbols = decimal->getDecimalFormatSymbols();
UnicodeString string = symbols->getSymbol(symbol);
BStringByteSink stringConverter(&result);
string.toUTF8(stringConverter);
return result;
}