Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/payments/CurrencyStringFormatter.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/payments/CurrencyStringFormatter.java b/chrome/android/java/src/org/chromium/chrome/browser/payments/CurrencyStringFormatter.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b5d1394031e6dfafa9db124dc6c147aadf721204 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/payments/CurrencyStringFormatter.java |
| @@ -0,0 +1,74 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser.payments; |
| + |
| +import java.text.DecimalFormatSymbols; |
| +import java.util.Currency; |
| + |
| +/** |
| + * Formatter for currency strings that can be too large to parse into numbers. |
| + */ |
| +public class CurrencyStringFormatter { |
| + /** |
| + * The symbol for the currency. For example, the symbol for "USD" is "$". |
| + */ |
| + private final String mCurrencySymbol; |
| + |
| + /** |
| + * The number grouping separator. For example, "," in US. 3-digit groups are assumed. |
| + */ |
| + private final char mGroupingSeparator; |
| + |
| + /** |
| + * The decimal separator. For example, "." in US and "," in France. |
| + */ |
| + private final char mMonetaryDecimalSeparator; |
| + |
| + /** |
| + * Builds the formatter for the given currency code and the current user locale. |
| + */ |
| + public CurrencyStringFormatter(String currencyCode) { |
| + mCurrencySymbol = Currency.getInstance(currencyCode).getSymbol(); |
|
Ted C
2016/04/22 03:59:37
any chance there is no Currency associated with cu
please use gerrit instead
2016/04/25 19:22:30
Yes, the spec does not limit to only valid ISO 421
|
| + DecimalFormatSymbols symbols = new DecimalFormatSymbols(); |
|
Ted C
2016/04/22 03:59:36
this looks to use the current user's locale. what
please use gerrit instead
2016/04/25 19:22:30
Added the following comment:
// Use the current l
|
| + mGroupingSeparator = symbols.getGroupingSeparator(); |
| + mMonetaryDecimalSeparator = symbols.getMonetaryDecimalSeparator(); |
| + } |
| + |
| + /** |
| + * Formats the currency string for display. Does not parse the string into a number, because it |
| + * can be too large. The number is formatted for the current locale and follows the symbol of |
| + * the currency code. |
| + * |
| + * @param number The number to format. Should be in "^-?[0-9]+(\.[0-9]+)?$" format. |
| + * https://w3c.github.io/browser-payment-api/specs/paymentrequest.html#currencyamount |
| + */ |
| + public String format(String number) { |
| + StringBuilder value = new StringBuilder(number); |
| + int decimalSeparator = value.indexOf("."); |
|
Ted C
2016/04/22 03:59:36
what happens if there are more than one .?
Should
please use gerrit instead
2016/04/25 19:22:30
More than one "." would be rejected in validation
|
| + if (decimalSeparator < 0) { |
| + String suffix = ".00"; |
| + value.append(suffix); |
| + decimalSeparator = value.length() - suffix.length(); |
| + } else if (decimalSeparator == value.length() - 2) { |
| + value.append('0'); |
| + } |
| + |
| + final int groupingSize = 3; |
| + for (int i = decimalSeparator - groupingSize; i > 0; i -= groupingSize) { |
| + if (value.charAt(i) == '-' || value.charAt(i - 1) == '-') { |
| + break; |
| + } |
| + |
| + value.insert(i, mGroupingSeparator); |
| + decimalSeparator++; |
| + } |
| + |
| + value.setCharAt(decimalSeparator, mMonetaryDecimalSeparator); |
| + value.insert(0, ' '); |
| + value.insert(0, mCurrencySymbol); |
| + |
| + return value.toString(); |
| + } |
| +} |