| 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 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 var intValue = number.truncate().toInt(); | 159 var intValue = number.truncate().toInt(); |
| 160 var multiplied = (number * power).round(); | 160 var multiplied = (number * power).round(); |
| 161 var fracValue = (multiplied - intValue * power).floor().toInt(); | 161 var fracValue = (multiplied - intValue * power).floor().toInt(); |
| 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.write(symbols.ZERO_DIGIT); |
| 170 intValue = intValue ~/ 10; | 170 intValue = intValue ~/ 10; |
| 171 } | 171 } |
| 172 var integerDigits = "${intValue}${paddingDigits}".codeUnits; | 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); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 | 220 |
| 221 /** | 221 /** |
| 222 * Create a new empty buffer. See comment on [_buffer] variable for why | 222 * Create a new empty buffer. See comment on [_buffer] variable for why |
| 223 * we have it as an instance variable rather than passing it on the stack. | 223 * we have it as an instance variable rather than passing it on the stack. |
| 224 */ | 224 */ |
| 225 void _newBuffer() { _buffer = new StringBuffer(); } | 225 void _newBuffer() { _buffer = new StringBuffer(); } |
| 226 | 226 |
| 227 /** A group of methods that provide support for writing digits and other | 227 /** A group of methods that provide support for writing digits and other |
| 228 * required characters into [_buffer] easily. | 228 * required characters into [_buffer] easily. |
| 229 */ | 229 */ |
| 230 void _add(String x) { _buffer.add(x);} | 230 void _add(String x) { _buffer.write(x);} |
| 231 void _addCharCode(int x) { _buffer.addCharCode(x); } | 231 void _addCharCode(int x) { _buffer.writeCharCode(x); } |
| 232 void _addZero() { _buffer.add(symbols.ZERO_DIGIT); } | 232 void _addZero() { _buffer.write(symbols.ZERO_DIGIT); } |
| 233 void _addDigit(int x) { _buffer.addCharCode(_localeZero + x - _zero); } | 233 void _addDigit(int x) { _buffer.writeCharCode(_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.codeUnits) { | 240 for (var x in basic.codeUnits) { |
| 241 _addDigit(x); | 241 _addDigit(x); |
| 242 } | 242 } |
| 243 } | 243 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after 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 |