NumberFormat: add floating-point precision support
- Simplify code in AboutSystem - Make error string translatable in Locale preflet Change-Id: I3b967ee04d764ec1bcf0fa40e40dae8da4a35c20 Reviewed-on: https://review.haiku-os.org/c/haiku/+/7373 Reviewed-by: waddlesplash <[email protected]>
This commit is contained in:
@@ -14,6 +14,7 @@ enum BNumberElement {
|
||||
B_GROUPING_SEPARATOR,
|
||||
};
|
||||
|
||||
|
||||
class BNumberFormatImpl;
|
||||
|
||||
|
||||
@@ -23,22 +24,18 @@ public:
|
||||
BNumberFormat(const BLocale* locale);
|
||||
~BNumberFormat();
|
||||
|
||||
ssize_t Format(char* string, size_t maxSize,
|
||||
const double value);
|
||||
ssize_t Format(char* string, size_t maxSize, const double value);
|
||||
status_t Format(BString& string, const double value);
|
||||
ssize_t Format(char* string, size_t maxSize,
|
||||
const int32 value);
|
||||
ssize_t Format(char* string, size_t maxSize, const int32 value);
|
||||
status_t Format(BString& string, const int32 value);
|
||||
|
||||
ssize_t FormatMonetary(char* string, size_t maxSize,
|
||||
const double value);
|
||||
status_t FormatMonetary(BString& string,
|
||||
const double value);
|
||||
status_t SetPrecision(int precision);
|
||||
|
||||
ssize_t FormatPercent(char* string, size_t maxSize,
|
||||
const double value);
|
||||
status_t FormatPercent(BString& string,
|
||||
const double value);
|
||||
ssize_t FormatMonetary(char* string, size_t maxSize, const double value);
|
||||
status_t FormatMonetary(BString& string, const double value);
|
||||
|
||||
ssize_t FormatPercent(char* string, size_t maxSize, const double value);
|
||||
status_t FormatPercent(BString& string, const double value);
|
||||
|
||||
status_t Parse(const BString& string, double& value);
|
||||
|
||||
|
||||
@@ -1189,12 +1189,15 @@ SysInfoView::_GetRamUsage(system_info* sysInfo)
|
||||
BString ramUsage;
|
||||
BString data;
|
||||
double usedMemoryPercent = double(sysInfo->used_pages) / sysInfo->max_pages;
|
||||
status_t status = fNumberFormat.FormatPercent(data, usedMemoryPercent);
|
||||
|
||||
if (fNumberFormat.FormatPercent(data, usedMemoryPercent) != B_OK)
|
||||
data.SetToFormat("%d%%", (int)(100 * usedMemoryPercent));
|
||||
|
||||
ramUsage.SetToFormat(B_TRANSLATE_COMMENT("%d MiB used (%s)",
|
||||
"326 MiB used (16%)"), used_pages(sysInfo), data.String());
|
||||
if (status == B_OK) {
|
||||
ramUsage.SetToFormat(B_TRANSLATE_COMMENT("%d MiB used (%s)",
|
||||
"326 MiB used (16%)"), used_pages(sysInfo), data.String());
|
||||
} else {
|
||||
ramUsage.SetToFormat(B_TRANSLATE_COMMENT("%d MiB used (%d%%)",
|
||||
"326 MiB used (16%)"), used_pages(sysInfo), (int)(100 * usedMemoryPercent));
|
||||
}
|
||||
|
||||
return ramUsage;
|
||||
}
|
||||
|
||||
@@ -278,6 +278,29 @@ BNumberFormat::Format(BString& string, const int32 value)
|
||||
}
|
||||
|
||||
|
||||
status_t
|
||||
BNumberFormat::SetPrecision(int precision)
|
||||
{
|
||||
NumberFormat* decimalFormatter = fPrivateData->GetFloat(&fConventions);
|
||||
NumberFormat* currencyFormatter = fPrivateData->GetCurrency(&fConventions);
|
||||
NumberFormat* percentFormatter = fPrivateData->GetPercent(&fConventions);
|
||||
|
||||
if ((decimalFormatter == NULL) || (currencyFormatter == NULL) || (percentFormatter == NULL))
|
||||
return B_ERROR;
|
||||
|
||||
decimalFormatter->setMinimumFractionDigits(precision);
|
||||
decimalFormatter->setMaximumFractionDigits(precision);
|
||||
|
||||
currencyFormatter->setMinimumFractionDigits(precision);
|
||||
currencyFormatter->setMaximumFractionDigits(precision);
|
||||
|
||||
percentFormatter->setMinimumFractionDigits(precision);
|
||||
percentFormatter->setMaximumFractionDigits(precision);
|
||||
|
||||
return B_OK;
|
||||
}
|
||||
|
||||
|
||||
ssize_t
|
||||
BNumberFormat::FormatMonetary(char* string, size_t maxSize, const double value)
|
||||
{
|
||||
|
||||
@@ -378,8 +378,9 @@ FormatSettingsView::_UpdateExamples()
|
||||
// Do NOT make these class members. We do want to recreate it everytime, as
|
||||
// to get the updated settings from the locale roster.
|
||||
BDateFormat dateFormat;
|
||||
BTimeFormat timeFormat;
|
||||
BNumberFormat numberFormat;
|
||||
BString errorString = B_TRANSLATE("ERROR");
|
||||
BTimeFormat timeFormat;
|
||||
|
||||
dateFormat.Format(result, timeValue, B_FULL_DATE_FORMAT);
|
||||
fFullDateExampleView->SetText(result);
|
||||
@@ -405,27 +406,27 @@ FormatSettingsView::_UpdateExamples()
|
||||
timeFormat.Format(result, timeValue, B_SHORT_TIME_FORMAT);
|
||||
fShortTimeExampleView->SetText(result);
|
||||
|
||||
status_t status = numberFormat.Format(result, 1234.5678);
|
||||
status_t status = numberFormat.Format(result, 1234.56);
|
||||
if (status == B_OK)
|
||||
fPositiveNumberExampleView->SetText(result);
|
||||
else
|
||||
fPositiveNumberExampleView->SetText("ERROR");
|
||||
fPositiveNumberExampleView->SetText(errorString);
|
||||
|
||||
status = numberFormat.Format(result, -1234.5678);
|
||||
status = numberFormat.Format(result, -1234.56);
|
||||
if (status == B_OK)
|
||||
fNegativeNumberExampleView->SetText(result);
|
||||
else
|
||||
fNegativeNumberExampleView->SetText("ERROR");
|
||||
fNegativeNumberExampleView->SetText(errorString);
|
||||
|
||||
status = numberFormat.FormatMonetary(result, 1234.56);
|
||||
if (status == B_OK)
|
||||
fPositiveMonetaryExampleView->SetText(result);
|
||||
else
|
||||
fPositiveMonetaryExampleView->SetText("ERROR");
|
||||
fPositiveMonetaryExampleView->SetText(errorString);
|
||||
|
||||
status = numberFormat.FormatMonetary(result, -1234.56);
|
||||
if (status == B_OK)
|
||||
fNegativeMonetaryExampleView->SetText(result);
|
||||
else
|
||||
fNegativeMonetaryExampleView->SetText("ERROR");
|
||||
fNegativeMonetaryExampleView->SetText(errorString);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user