Today I have discovered a javascript tool named numeraljs. It is really useful to format numeric values. You know, this is really strain things for we developers. I hope, you can solve your problems occured by numerics.
Let's see some examples: (For details, http://numeraljs.com/)
I totally copied these informations below from the project site.
| Number | Format | String |
|---|---|---|
| 10000 | '0,0.0000' | 10.000,0000 |
| 10000.23 | '0,0' | 10.000 |
| 10000.23 | '+0,0' | +10.000 |
| -10000 | '0,0.0' | -10.000,0 |
| 10000.1234 | '0.000' | 10000,123 |
| 10000.1234 | '0[.]00000' | 10000,12340 |
| -10000 | '(0,0.0000)' | (10.000,0000) |
| -0.23 | '.00' | -,23 |
| -0.23 | '(.00)' | (,23) |
| 0.23 | '0.00000' | 0,23000 |
| 0.23 | '0.0[0000]' | 0,23 |
| 1230974 | '0.0a' | 1,2milyon |
| 1460 | '0 a' | 1 bin |
| -104000 | '0a' | -104bin |
| 1 | '0o' | 1'inci |
| 52 | '0o' | 52'nci |
| 23 | '0o' | 23'üncü |
| 100 | '0o' | 100'üncü |
Currency
| Number | Format | String |
|---|---|---|
| 1000.234 | '$0,0.00' | ₺1.000,23 |
| 1000.2 | '0,0[.]00 $' | 1.000,20 ₺ |
| 1001 | '$ 0,0[.]00' | ₺ 1.001 |
| -1000.234 | '($0,0)' | (₺1.000) |
| -1000.234 | '$0.00' | -₺1000,23 |
| 1230974 | '($ 0.00 a)' | ₺ 1,23 milyon |
Bytes
| Number | Format | String |
|---|---|---|
| 100 | '0b' | 100B |
| 2048 | '0 b' | 2 KB |
| 7884486213 | '0.0b' | 7,3GB |
| 3467479682787 | '0.000 b' | 3,154 TB |
Percentages
| Number | Format | String |
|---|---|---|
| 1 | '0%' | 100% |
| 0.974878234 | '0.000%' | 97,488% |
| -0.43 | '0 %' | -43 % |
| 0.43 | '(0.000 %)' | 43,000 % |
Time
| Number | Format | String |
|---|---|---|
| 25 | '00:00:00' | 0:00:25 |
| 238 | '00:00:00' | 0:03:58 |
| 63846 | '00:00:00' | 17:44:06 |
Unformat
Got a formatted string? Use the unformat function to make it useful again.
var string = numeral().unformat('($10,000.00)');
// -10000
| String | Function | Number |
|---|---|---|
| '10.000,123' | .unformat('10.000,123') | 10000.123 |
| '0,12345' | .unformat('0,12345') | 0.12345 |
| '1,23milyon' | .unformat('1,23milyon') | 1230000 |
| '23'üncü' | .unformat('23'üncü') | 23 |
| '₺10.000,00' | .unformat('₺10.000,00') | 10000 |
| '100B' | .unformat('100B') | 100 |
| '3,154TB' | .unformat('3,154TB') | 3467859674006 |
| '-76%' | .unformat('-76%') | -0.76 |
| '2:23:57' | .unformat('2:23:57') | 8637 |
Manipulate
Not that you will use these often, but they're there when you need them.
var number = numeral(1000); var added = number.add(10); // 1010
| Before | Function | After |
|---|---|---|
| 1000 | .add(100) | 1100 |
| 1100 | .subtract(100) | 1000 |
| 1000 | .multiply(100) | 100000 |
| 100000 | .divide(100) | 1000 |
Value
The value is always available.
var number = numeral(1000);
var string = number.format('0,0');
// '1,000'
var value = number.value();
// 1000
Set
Set the value of your numeral object.
var number = numeral(); number.set(1000); var value = number.value(); // 1000
Difference
Find the difference between your numeral object and a value
var number = numeral(1000),
value = 100;
var difference = number.difference(value);
// 900
Default Formatting
Set a default format so you can use .format() without a string. The default format to '0,0'
var number = numeral(1000);
number.format();
// '1,000'
numeral.defaultFormat('$0,0.00');
number.format();
// '$1,000.00'
Custom Zero Formatting
Set a custom output when formatting numerals with a value of 0
var number = numeral(0);
numeral.zeroFormat('N/A');
var zero = number.format('0.0')
// 'N/A'
Clone
Go ahead and clone any numeral object while you're at it.
var a = numeral(1000); var b = numeral(a); var c = a.clone(); var aVal = a.set(2000).value(); // 2000 var bVal = b.value(); // 1000 var cVal = c.add(10).value(); // 1010
For details, http://numeraljs.com/
Hiç yorum yok:
Yorum Gönder