| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. All rights reserved. | 2 * Copyright 2014 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style | 4 * Use of this source code is governed by a BSD-style |
| 5 * license that can be found in the LICENSE file or at | 5 * license that can be found in the LICENSE file or at |
| 6 * https://developers.google.com/open-source/licenses/bsd | 6 * https://developers.google.com/open-source/licenses/bsd |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 library charted.locale.format; | 9 library charted.locale.format; |
| 10 | 10 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 * | 28 * |
| 29 * @see <a href="http://docs.python.org/release/3.1.3/library/string.html#format
spec">Python format specification mini-language</a> | 29 * @see <a href="http://docs.python.org/release/3.1.3/library/string.html#format
spec">Python format specification mini-language</a> |
| 30 */ | 30 */ |
| 31 FormatFunction format(String specifier, [Locale locale = null]) { | 31 FormatFunction format(String specifier, [Locale locale = null]) { |
| 32 if (locale == null) { | 32 if (locale == null) { |
| 33 locale = new EnUsLocale(); | 33 locale = new EnUsLocale(); |
| 34 } | 34 } |
| 35 return locale.numberFormat.format(specifier); | 35 return locale.numberFormat.format(specifier); |
| 36 } | 36 } |
| 37 | 37 |
| 38 | |
| 39 /* | 38 /* |
| 40 * Class for computing the SI format prefix for the given value. | 39 * Class for computing the SI format prefix for the given value. |
| 41 */ | 40 */ |
| 42 class FormatPrefix { | 41 class FormatPrefix { |
| 43 // SI scale units in increments of 1000. | 42 // SI scale units in increments of 1000. |
| 44 static const List unitPrefixes = const | 43 static const List unitPrefixes = const [ |
| 45 ["y","z","a","f","p","n","µ","m","","k","M","G","T","P","E","Z","Y"]; | 44 "y", |
| 45 "z", |
| 46 "a", |
| 47 "f", |
| 48 "p", |
| 49 "n", |
| 50 "µ", |
| 51 "m", |
| 52 "", |
| 53 "k", |
| 54 "M", |
| 55 "G", |
| 56 "T", |
| 57 "P", |
| 58 "E", |
| 59 "Z", |
| 60 "Y" |
| 61 ]; |
| 46 | 62 |
| 47 Function _scale; | 63 Function _scale; |
| 48 String _symbol; | 64 String _symbol; |
| 49 | 65 |
| 50 FormatPrefix(num value, [int precision = 0]) { | 66 FormatPrefix(num value, [int precision = 0]) { |
| 51 var i = 0; | 67 var i = 0; |
| 52 if (value < 0) { | 68 if (value < 0) { |
| 53 value *= -1; | 69 value *= -1; |
| 54 } | 70 } |
| 55 if (precision > 0) { | 71 if (precision > 0) { |
| 56 value = _roundToPrecision(value, _formatPrecision(value, precision)); | 72 value = _roundToPrecision(value, _formatPrecision(value, precision)); |
| 57 } | 73 } |
| 58 | 74 |
| 59 // Determining SI scale of the value in increment of 1000. | 75 // Determining SI scale of the value in increment of 1000. |
| 60 i = 1 + (1e-12 + math.log(value) / math.LN10).floor(); | 76 i = 1 + (1e-12 + math.log(value) / math.LN10).floor(); |
| 61 i = math.max(-24, math.min(24, | 77 i = math.max(-24, math.min(24, ((i - 1) / 3).floor() * 3)); |
| 62 ((i - 1) / 3).floor() * 3)); | |
| 63 i = 8 + (i / 3).floor(); | 78 i = 8 + (i / 3).floor(); |
| 64 | 79 |
| 65 // Sets the scale and symbol of the value. | 80 // Sets the scale and symbol of the value. |
| 66 var k = math.pow(10, (8 - i).abs() * 3); | 81 var k = math.pow(10, (8 - i).abs() * 3); |
| 67 _scale = i > 8 ? (d) => d / k : (d) => d * k; | 82 _scale = i > 8 ? (d) => d / k : (d) => d * k; |
| 68 _symbol = unitPrefixes[i]; | 83 _symbol = unitPrefixes[i]; |
| 69 } | 84 } |
| 70 | 85 |
| 71 _formatPrecision(num x, num p) { | 86 _formatPrecision(num x, num p) { |
| 72 return p - (x != 0 ? (math.log(x) / math.LN10).ceil() : 1); | 87 return p - (x != 0 ? (math.log(x) / math.LN10).ceil() : 1); |
| 73 } | 88 } |
| 74 | 89 |
| 75 /** Returns the value of x rounded to the nth digit. */ | 90 /** Returns the value of x rounded to the nth digit. */ |
| 76 _roundToPrecision(num x, num n) { | 91 _roundToPrecision(num x, num n) { |
| 77 return n != 0 ? | 92 return n != 0 ? (x * (n = math.pow(10, n))).round() / n : x.round(); |
| 78 (x * (n = math.pow(10, n))).round() / n : x.round(); | |
| 79 } | 93 } |
| 80 | 94 |
| 81 /** Returns the SI prefix for the value. */ | 95 /** Returns the SI prefix for the value. */ |
| 82 get symbol => _symbol; | 96 get symbol => _symbol; |
| 83 | 97 |
| 84 /** Returns the scale for the value corresponding to the SI prefix. */ | 98 /** Returns the scale for the value corresponding to the SI prefix. */ |
| 85 get scale => _scale; | 99 get scale => _scale; |
| 86 } | 100 } |
| OLD | NEW |