| 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}".charCodes; |
| 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.charCodes; |
| 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.charCodes) { |
| 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. |
| 251 */ | 251 */ |
| 252 void _group(int totalLength, int position) { | 252 void _group(int totalLength, int position) { |
| 253 var distanceFromEnd = totalLength - position; | 253 var distanceFromEnd = totalLength - position; |
| 254 if (distanceFromEnd <= 1 || _groupingSize <= 0) return; | 254 if (distanceFromEnd <= 1 || _groupingSize <= 0) return; |
| 255 if (distanceFromEnd % _groupingSize == 1) { | 255 if (distanceFromEnd % _groupingSize == 1) { |
| 256 _add(symbols.GROUP_SEP); | 256 _add(symbols.GROUP_SEP); |
| 257 } | 257 } |
| 258 } | 258 } |
| 259 | 259 |
| 260 /** Returns the code point for the character '0'. */ | 260 /** Returns the code point for the character '0'. */ |
| 261 int get _zero => '0'.charCodes()[0]; | 261 int get _zero => '0'.charCodes[0]; |
| 262 | 262 |
| 263 /** Returns the code point for the locale's zero digit. */ | 263 /** Returns the code point for the locale's zero digit. */ |
| 264 int get _localeZero => symbols.ZERO_DIGIT.charCodeAt(0); | 264 int get _localeZero => symbols.ZERO_DIGIT.charCodeAt(0); |
| 265 | 265 |
| 266 /** | 266 /** |
| 267 * Returns the prefix for [x] based on whether it's positive or negative. | 267 * Returns the prefix for [x] based on whether it's positive or negative. |
| 268 * In en_US this would be '' and '-' respectively. | 268 * In en_US this would be '' and '-' respectively. |
| 269 */ | 269 */ |
| 270 String _signPrefix(num x) { | 270 String _signPrefix(num x) { |
| 271 return x.isNegative ? _negativePrefix : _positivePrefix; | 271 return x.isNegative ? _negativePrefix : _positivePrefix; |
| 272 } | 272 } |
| 273 | 273 |
| 274 /** | 274 /** |
| 275 * Returns the suffix for [x] based on wether it's positive or negative. | 275 * Returns the suffix for [x] based on wether it's positive or negative. |
| 276 * In en_US there are no suffixes for positive or negative. | 276 * In en_US there are no suffixes for positive or negative. |
| 277 */ | 277 */ |
| 278 String _signSuffix(num x) { | 278 String _signSuffix(num x) { |
| 279 return x.isNegative ? _negativeSuffix : _positiveSuffix; | 279 return x.isNegative ? _negativeSuffix : _positiveSuffix; |
| 280 } | 280 } |
| 281 } | 281 } |
| OLD | NEW |