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,
|
B_GROUPING_SEPARATOR,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class BNumberFormatImpl;
|
class BNumberFormatImpl;
|
||||||
|
|
||||||
|
|
||||||
@@ -23,22 +24,18 @@ public:
|
|||||||
BNumberFormat(const BLocale* locale);
|
BNumberFormat(const BLocale* locale);
|
||||||
~BNumberFormat();
|
~BNumberFormat();
|
||||||
|
|
||||||
ssize_t Format(char* string, size_t maxSize,
|
ssize_t Format(char* string, size_t maxSize, const double value);
|
||||||
const double value);
|
|
||||||
status_t Format(BString& string, const double value);
|
status_t Format(BString& string, const double value);
|
||||||
ssize_t Format(char* string, size_t maxSize,
|
ssize_t Format(char* string, size_t maxSize, const int32 value);
|
||||||
const int32 value);
|
|
||||||
status_t Format(BString& string, const int32 value);
|
status_t Format(BString& string, const int32 value);
|
||||||
|
|
||||||
ssize_t FormatMonetary(char* string, size_t maxSize,
|
status_t SetPrecision(int precision);
|
||||||
const double value);
|
|
||||||
status_t FormatMonetary(BString& string,
|
|
||||||
const double value);
|
|
||||||
|
|
||||||
ssize_t FormatPercent(char* string, size_t maxSize,
|
ssize_t FormatMonetary(char* string, size_t maxSize, const double value);
|
||||||
const double value);
|
status_t FormatMonetary(BString& string, const double value);
|
||||||
status_t FormatPercent(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);
|
status_t Parse(const BString& string, double& value);
|
||||||
|
|
||||||
|
|||||||
@@ -1189,12 +1189,15 @@ SysInfoView::_GetRamUsage(system_info* sysInfo)
|
|||||||
BString ramUsage;
|
BString ramUsage;
|
||||||
BString data;
|
BString data;
|
||||||
double usedMemoryPercent = double(sysInfo->used_pages) / sysInfo->max_pages;
|
double usedMemoryPercent = double(sysInfo->used_pages) / sysInfo->max_pages;
|
||||||
|
status_t status = fNumberFormat.FormatPercent(data, usedMemoryPercent);
|
||||||
|
|
||||||
if (fNumberFormat.FormatPercent(data, usedMemoryPercent) != B_OK)
|
if (status == 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());
|
||||||
ramUsage.SetToFormat(B_TRANSLATE_COMMENT("%d MiB used (%s)",
|
} else {
|
||||||
"326 MiB used (16%)"), used_pages(sysInfo), data.String());
|
ramUsage.SetToFormat(B_TRANSLATE_COMMENT("%d MiB used (%d%%)",
|
||||||
|
"326 MiB used (16%)"), used_pages(sysInfo), (int)(100 * usedMemoryPercent));
|
||||||
|
}
|
||||||
|
|
||||||
return ramUsage;
|
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
|
ssize_t
|
||||||
BNumberFormat::FormatMonetary(char* string, size_t maxSize, const double value)
|
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
|
// Do NOT make these class members. We do want to recreate it everytime, as
|
||||||
// to get the updated settings from the locale roster.
|
// to get the updated settings from the locale roster.
|
||||||
BDateFormat dateFormat;
|
BDateFormat dateFormat;
|
||||||
BTimeFormat timeFormat;
|
|
||||||
BNumberFormat numberFormat;
|
BNumberFormat numberFormat;
|
||||||
|
BString errorString = B_TRANSLATE("ERROR");
|
||||||
|
BTimeFormat timeFormat;
|
||||||
|
|
||||||
dateFormat.Format(result, timeValue, B_FULL_DATE_FORMAT);
|
dateFormat.Format(result, timeValue, B_FULL_DATE_FORMAT);
|
||||||
fFullDateExampleView->SetText(result);
|
fFullDateExampleView->SetText(result);
|
||||||
@@ -405,27 +406,27 @@ FormatSettingsView::_UpdateExamples()
|
|||||||
timeFormat.Format(result, timeValue, B_SHORT_TIME_FORMAT);
|
timeFormat.Format(result, timeValue, B_SHORT_TIME_FORMAT);
|
||||||
fShortTimeExampleView->SetText(result);
|
fShortTimeExampleView->SetText(result);
|
||||||
|
|
||||||
status_t status = numberFormat.Format(result, 1234.5678);
|
status_t status = numberFormat.Format(result, 1234.56);
|
||||||
if (status == B_OK)
|
if (status == B_OK)
|
||||||
fPositiveNumberExampleView->SetText(result);
|
fPositiveNumberExampleView->SetText(result);
|
||||||
else
|
else
|
||||||
fPositiveNumberExampleView->SetText("ERROR");
|
fPositiveNumberExampleView->SetText(errorString);
|
||||||
|
|
||||||
status = numberFormat.Format(result, -1234.5678);
|
status = numberFormat.Format(result, -1234.56);
|
||||||
if (status == B_OK)
|
if (status == B_OK)
|
||||||
fNegativeNumberExampleView->SetText(result);
|
fNegativeNumberExampleView->SetText(result);
|
||||||
else
|
else
|
||||||
fNegativeNumberExampleView->SetText("ERROR");
|
fNegativeNumberExampleView->SetText(errorString);
|
||||||
|
|
||||||
status = numberFormat.FormatMonetary(result, 1234.56);
|
status = numberFormat.FormatMonetary(result, 1234.56);
|
||||||
if (status == B_OK)
|
if (status == B_OK)
|
||||||
fPositiveMonetaryExampleView->SetText(result);
|
fPositiveMonetaryExampleView->SetText(result);
|
||||||
else
|
else
|
||||||
fPositiveMonetaryExampleView->SetText("ERROR");
|
fPositiveMonetaryExampleView->SetText(errorString);
|
||||||
|
|
||||||
status = numberFormat.FormatMonetary(result, -1234.56);
|
status = numberFormat.FormatMonetary(result, -1234.56);
|
||||||
if (status == B_OK)
|
if (status == B_OK)
|
||||||
fNegativeMonetaryExampleView->SetText(result);
|
fNegativeMonetaryExampleView->SetText(result);
|
||||||
else
|
else
|
||||||
fNegativeMonetaryExampleView->SetText("ERROR");
|
fNegativeMonetaryExampleView->SetText(errorString);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user