AI2 Component  (Version nb184)
YailNumberToString.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2009-2011 Google, All Rights reserved
3 // Copyright 2011-2012 MIT, All rights reserved
4 // Released under the Apache License, Version 2.0
5 // http://www.apache.org/licenses/LICENSE-2.0
6 
7 package com.google.appinventor.components.runtime.util;
8 
9 import android.util.Log;
10 
11 
12 import java.text.*;
13 import java.util.Locale;
14 
21 public final class YailNumberToString {
22 
23  static final String LOG_TAG = "YailNumberToString";
24 
25  // format magnitudes larger than BIGBOUND in scientific notation
26  private static final double BIGBOUND = 1.e6;
27  // format magnitudes smaller than SMALLBOUND in scientific notation
28  private static final double SMALLBOUND = 1.e-6;
29 
30 
31  // TODO(halabelson): The Java documentation warns that formatters are
32  // not thread-safe. Is there any way that this can bite us?
33  // format for decimal notation
34  private static final String decPattern = "#####0.0####";
35  // format for scientific notation
36  private static final String sciPattern = "0.####E0";
37 
38  // TODO(hal): We are making the decimal separator be a period, regardless of
39  // the locale of the phone. We need to think about how to allow comma as decimal separator,
40  // which will require updating number parsing and other places that transform numbers to strings,
41  // such as FormatAsDecimal
42 
43  static Locale locale = Locale.US;
44  static DecimalFormatSymbols symbols = new DecimalFormatSymbols(locale);
45 
46  static DecimalFormat decimalFormat = new DecimalFormat(decPattern, symbols);
47  static DecimalFormat sciFormat = new DecimalFormat(sciPattern, symbols);
48 
49 
50  // TODO(halabelson): DecimalFormat scientific notation apparently does not provide a
51  // way to specify the maximum number of digits in the mantissa. One consequence is that this
52  // formatting method, when given floats of very large magnitude, will produce too many
53  // places in the mantissa. Consider post-processing the result of
54  // DecimalFormat to remove these extra digits.
55 
56  // This implementation assumes that Kawa inexact numbers are passed to this routine
57  // as doubles.
58  public static String format(double number) {
59  // Handle positive and negative infinities (which are "round" and therefore coerce to long)
60  if (Double.isInfinite(number)) {
61  if (number < 0.0) {
62  return "-infinity";
63  } else {
64  return "+infinity";
65  }
66  }
67  // We will print integer values without a decimal point.
68  if (number == Math.rint(number)) {
69  return String.valueOf((long) number);
70  } else {
71  double mag = Math.abs(number);
72  if (mag < BIGBOUND && mag > SMALLBOUND) {
73  return decimalFormat.format(number);
74  } else {
75  return sciFormat.format(number);
76  }
77  }
78  }
79 }
com.google.appinventor.components.runtime.util.YailNumberToString.format
static String format(double number)
Definition: YailNumberToString.java:58
com.google.appinventor.components.runtime.util.YailNumberToString
Definition: YailNumberToString.java:21