| 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'; | 7 import 'dart:math'; |
| 8 | 8 |
| 9 import "intl.dart"; | 9 import "intl.dart"; |
| 10 import "number_symbols.dart"; | 10 import "number_symbols.dart"; |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 var fractionPresent = _minimumFractionDigits > 0 || fracValue > 0; | 162 var fractionPresent = _minimumFractionDigits > 0 || fracValue > 0; |
| 163 | 163 |
| 164 // On dartj2s the integer part may be large enough to be a floating | 164 // On dartj2s the integer part may be large enough to be a floating |
| 165 // point value, in which case we reduce it until it is small enough | 165 // point value, in which case we reduce it until it is small enough |
| 166 // to be printed as an integer and pad the remainder with zeros. | 166 // to be printed as an integer and pad the remainder with zeros. |
| 167 var paddingDigits = new StringBuffer(); | 167 var paddingDigits = new StringBuffer(); |
| 168 while ((intValue & 0x7fffffff) != intValue) { | 168 while ((intValue & 0x7fffffff) != intValue) { |
| 169 paddingDigits.add(symbols.ZERO_DIGIT); | 169 paddingDigits.add(symbols.ZERO_DIGIT); |
| 170 intValue = intValue ~/ 10; | 170 intValue = intValue ~/ 10; |
| 171 } | 171 } |
| 172 var integerDigits = "${intValue}${paddingDigits}".charCodes; | 172 var integerDigits = "${intValue}${paddingDigits}".codeUnits; |
| 173 var digitLength = integerDigits.length; | 173 var digitLength = integerDigits.length; |
| 174 | 174 |
| 175 if (_hasPrintableIntegerPart(intValue)) { | 175 if (_hasPrintableIntegerPart(intValue)) { |
| 176 _pad(_minimumIntegerDigits - digitLength); | 176 _pad(_minimumIntegerDigits - digitLength); |
| 177 for (var i = 0; i < digitLength; i++) { | 177 for (var i = 0; i < digitLength; i++) { |
| 178 _addDigit(integerDigits[i]); | 178 _addDigit(integerDigits[i]); |
| 179 _group(digitLength, i); | 179 _group(digitLength, i); |
| 180 } | 180 } |
| 181 } else if (!fractionPresent) { | 181 } else if (!fractionPresent) { |
| 182 // If neither fraction nor integer part exists, just print zero. | 182 // If neither fraction nor integer part exists, just print zero. |
| 183 _addZero(); | 183 _addZero(); |
| 184 } | 184 } |
| 185 | 185 |
| 186 _decimalSeparator(fractionPresent); | 186 _decimalSeparator(fractionPresent); |
| 187 _formatFractionPart((fracValue + power).toString()); | 187 _formatFractionPart((fracValue + power).toString()); |
| 188 } | 188 } |
| 189 | 189 |
| 190 /** | 190 /** |
| 191 * Format the part after the decimal place in a fixed point number. | 191 * Format the part after the decimal place in a fixed point number. |
| 192 */ | 192 */ |
| 193 void _formatFractionPart(String fractionPart) { | 193 void _formatFractionPart(String fractionPart) { |
| 194 var fractionCodes = fractionPart.charCodes; | 194 var fractionCodes = fractionPart.codeUnits; |
| 195 var fractionLength = fractionPart.length; | 195 var fractionLength = fractionPart.length; |
| 196 while (fractionPart[fractionLength - 1] == '0' && | 196 while (fractionPart[fractionLength - 1] == '0' && |
| 197 fractionLength > _minimumFractionDigits + 1) { | 197 fractionLength > _minimumFractionDigits + 1) { |
| 198 fractionLength--; | 198 fractionLength--; |
| 199 } | 199 } |
| 200 for (var i = 1; i < fractionLength; i++) { | 200 for (var i = 1; i < fractionLength; i++) { |
| 201 _addDigit(fractionCodes[i]); | 201 _addDigit(fractionCodes[i]); |
| 202 } | 202 } |
| 203 } | 203 } |
| 204 | 204 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 230 void _add(String x) { _buffer.add(x);} | 230 void _add(String x) { _buffer.add(x);} |
| 231 void _addCharCode(int x) { _buffer.addCharCode(x); } | 231 void _addCharCode(int x) { _buffer.addCharCode(x); } |
| 232 void _addZero() { _buffer.add(symbols.ZERO_DIGIT); } | 232 void _addZero() { _buffer.add(symbols.ZERO_DIGIT); } |
| 233 void _addDigit(int x) { _buffer.addCharCode(_localeZero + x - _zero); } | 233 void _addDigit(int x) { _buffer.addCharCode(_localeZero + x - _zero); } |
| 234 | 234 |
| 235 /** Print padding up to [numberOfDigits] above what's included in [basic]. */ | 235 /** Print padding up to [numberOfDigits] above what's included in [basic]. */ |
| 236 void _pad(int numberOfDigits, [String basic = '']) { | 236 void _pad(int numberOfDigits, [String basic = '']) { |
| 237 for (var i = 0; i < numberOfDigits - basic.length; i++) { | 237 for (var i = 0; i < numberOfDigits - basic.length; i++) { |
| 238 _add(symbols.ZERO_DIGIT); | 238 _add(symbols.ZERO_DIGIT); |
| 239 } | 239 } |
| 240 for (var x in basic.charCodes) { | 240 for (var x in basic.codeUnits) { |
| 241 _addDigit(x); | 241 _addDigit(x); |
| 242 } | 242 } |
| 243 } | 243 } |
| 244 | 244 |
| 245 /** | 245 /** |
| 246 * We are printing the digits of the number from left to right. We may need | 246 * We are printing the digits of the number from left to right. We may need |
| 247 * to print a thousands separator or other grouping character as appropriate | 247 * to print a thousands separator or other grouping character as appropriate |
| 248 * to the locale. So we find how many places we are from the end of the number | 248 * to the locale. So we find how many places we are from the end of the number |
| 249 * by subtracting our current [position] from the [totalLength] and print | 249 * by subtracting our current [position] from the [totalLength] and print |
| 250 * the separator character every [_groupingSize] digits. | 250 * the separator character every [_groupingSize] digits. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 276 } | 276 } |
| 277 | 277 |
| 278 /** | 278 /** |
| 279 * Returns the suffix for [x] based on wether it's positive or negative. | 279 * Returns the suffix for [x] based on wether it's positive or negative. |
| 280 * In en_US there are no suffixes for positive or negative. | 280 * In en_US there are no suffixes for positive or negative. |
| 281 */ | 281 */ |
| 282 String _signSuffix(num x) { | 282 String _signSuffix(num x) { |
| 283 return x.isNegative ? _negativeSuffix : _positiveSuffix; | 283 return x.isNegative ? _negativeSuffix : _positiveSuffix; |
| 284 } | 284 } |
| 285 } | 285 } |
| OLD | NEW |