| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #library("number_format"); | 5 #library("number_format"); |
| 6 | 6 |
| 7 #import('dart:math'); |
| 8 |
| 7 #import("intl.dart"); | 9 #import("intl.dart"); |
| 8 #import("number_symbols.dart"); | 10 #import("number_symbols.dart"); |
| 9 #import("number_symbols_data.dart"); | 11 #import("number_symbols_data.dart"); |
| 10 | 12 |
| 11 class NumberFormat { | 13 class NumberFormat { |
| 12 /** Variables to determine how number printing behaves. */ | 14 /** Variables to determine how number printing behaves. */ |
| 13 // TODO(alanknight): If these remain as variables and are set based on the | 15 // TODO(alanknight): If these remain as variables and are set based on the |
| 14 // pattern, can we make them final? | 16 // pattern, can we make them final? |
| 15 String _negativePrefix = '-'; | 17 String _negativePrefix = '-'; |
| 16 String _positivePrefix = ''; | 18 String _positivePrefix = ''; |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 } | 106 } |
| 105 | 107 |
| 106 /** Format the number in exponential notation. */ | 108 /** Format the number in exponential notation. */ |
| 107 _formatExponential(num number) { | 109 _formatExponential(num number) { |
| 108 if (number == 0.0) { | 110 if (number == 0.0) { |
| 109 _formatFixed(number); | 111 _formatFixed(number); |
| 110 _formatExponent(0); | 112 _formatExponent(0); |
| 111 return; | 113 return; |
| 112 } | 114 } |
| 113 | 115 |
| 114 var exponent = (Math.log(number) / Math.log(10)).floor(); | 116 var exponent = (log(number) / log(10)).floor(); |
| 115 var mantissa = number / Math.pow(10, exponent); | 117 var mantissa = number / pow(10, exponent); |
| 116 | 118 |
| 117 if (_minimumIntegerDigits < 1) { | 119 if (_minimumIntegerDigits < 1) { |
| 118 exponent++; | 120 exponent++; |
| 119 mantissa /= 10; | 121 mantissa /= 10; |
| 120 } else { | 122 } else { |
| 121 exponent -= _minimumIntegerDigits - 1; | 123 exponent -= _minimumIntegerDigits - 1; |
| 122 mantissa *= Math.pow(10, _minimumIntegerDigits - 1); | 124 mantissa *= pow(10, _minimumIntegerDigits - 1); |
| 123 } | 125 } |
| 124 _formatFixed(number); | 126 _formatFixed(number); |
| 125 _formatExponent(exponent); | 127 _formatExponent(exponent); |
| 126 } | 128 } |
| 127 | 129 |
| 128 /** | 130 /** |
| 129 * Format the exponent portion, e.g. in "1.3e-5" the "e-5". | 131 * Format the exponent portion, e.g. in "1.3e-5" the "e-5". |
| 130 */ | 132 */ |
| 131 void _formatExponent(num exponent) { | 133 void _formatExponent(num exponent) { |
| 132 _add(symbols.EXP_SYMBOL); | 134 _add(symbols.EXP_SYMBOL); |
| 133 if (exponent < 0) { | 135 if (exponent < 0) { |
| 134 exponent = -exponent; | 136 exponent = -exponent; |
| 135 _add(symbols.MINUS_SIGN); | 137 _add(symbols.MINUS_SIGN); |
| 136 } else if (_useSignForPositiveExponent) { | 138 } else if (_useSignForPositiveExponent) { |
| 137 _add(symbols.PLUS_SIGN); | 139 _add(symbols.PLUS_SIGN); |
| 138 } | 140 } |
| 139 _pad(_minimumExponentDigits, exponent.toString()); | 141 _pad(_minimumExponentDigits, exponent.toString()); |
| 140 } | 142 } |
| 141 | 143 |
| 142 /** | 144 /** |
| 143 * Format the basic number portion, inluding the fractional digits. | 145 * Format the basic number portion, inluding the fractional digits. |
| 144 */ | 146 */ |
| 145 void _formatFixed(num number) { | 147 void _formatFixed(num number) { |
| 146 // Round the number. | 148 // Round the number. |
| 147 var power = Math.pow(10, _maximumFractionDigits); | 149 var power = pow(10, _maximumFractionDigits); |
| 148 var intValue = number.truncate().toInt(); | 150 var intValue = number.truncate().toInt(); |
| 149 var multiplied = (number * power).round(); | 151 var multiplied = (number * power).round(); |
| 150 var fracValue = (multiplied - intValue * power).floor().toInt(); | 152 var fracValue = (multiplied - intValue * power).floor().toInt(); |
| 151 var fractionPresent = _minimumFractionDigits > 0 || fracValue > 0; | 153 var fractionPresent = _minimumFractionDigits > 0 || fracValue > 0; |
| 152 | 154 |
| 153 // On dartj2s the integer part may be large enough to be a floating | 155 // On dartj2s the integer part may be large enough to be a floating |
| 154 // point value, in which case we reduce it until it is small enough | 156 // point value, in which case we reduce it until it is small enough |
| 155 // to be printed as an integer and pad the remainder with zeros. | 157 // to be printed as an integer and pad the remainder with zeros. |
| 156 var paddingDigits = new StringBuffer(); | 158 var paddingDigits = new StringBuffer(); |
| 157 while (intValue is! int) { | 159 while (intValue is! int) { |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 } | 263 } |
| 262 | 264 |
| 263 /** | 265 /** |
| 264 * Returns the suffix for [x] based on wether it's positive or negative. | 266 * Returns the suffix for [x] based on wether it's positive or negative. |
| 265 * In en_US there are no suffixes for positive or negative. | 267 * In en_US there are no suffixes for positive or negative. |
| 266 */ | 268 */ |
| 267 String _signSuffix(num x) { | 269 String _signSuffix(num x) { |
| 268 return x.isNegative() ? _negativeSuffix : _positiveSuffix; | 270 return x.isNegative() ? _negativeSuffix : _positiveSuffix; |
| 269 } | 271 } |
| 270 } | 272 } |
| OLD | NEW |