Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1094)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/payments/CurrencyStringFormatter.java

Issue 1904553003: Java implementation of PaymentRequest mojo service (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Disable Pay button before user provided required information. Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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();
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698