BNumberFormat: add Parse() and GetSymbol().
This commit is contained in:
@@ -10,10 +10,8 @@
|
||||
|
||||
|
||||
enum BNumberElement {
|
||||
B_NUMBER_ELEMENT_INVALID = B_BAD_DATA,
|
||||
B_NUMBER_ELEMENT_INTEGER = 0,
|
||||
B_NUMBER_ELEMENT_FRACTIONAL,
|
||||
B_NUMBER_ELEMENT_CURRENCY
|
||||
B_DECIMAL_SEPARATOR = 10, // Values 0-9 are reserved for digit symbols
|
||||
B_GROUPING_SEPARATOR,
|
||||
};
|
||||
|
||||
class BNumberFormatImpl;
|
||||
@@ -36,6 +34,10 @@ public:
|
||||
status_t FormatMonetary(BString& string,
|
||||
const double value);
|
||||
|
||||
status_t Parse(const BString& string, double& value);
|
||||
|
||||
BString GetSeparator(BNumberElement element);
|
||||
|
||||
private:
|
||||
BNumberFormat(const BNumberFormat &other);
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
* Copyright 2003, Axel Dörfler, [email protected].
|
||||
* Copyright 2010-2011, Oliver Tappe, [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.
|
||||
*/
|
||||
|
||||
@@ -15,6 +16,8 @@
|
||||
|
||||
#include <ICUWrapper.h>
|
||||
|
||||
#include <unicode/dcfmtsym.h>
|
||||
#include <unicode/decimfmt.h>
|
||||
#include <unicode/numfmt.h>
|
||||
|
||||
|
||||
@@ -231,3 +234,60 @@ BNumberFormat::FormatMonetary(BString& string, const double value)
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user