adamwdraper / numeral-js Goto Github PK
View Code? Open in Web Editor NEWA javascript library for formatting and manipulating numbers.
Home Page: http://numeraljs.com
License: MIT License
A javascript library for formatting and manipulating numbers.
Home Page: http://numeraljs.com
License: MIT License
Where is 'defaultFormat' set ?
https://github.com/adamwdraper/Numeral-js/blob/master/numeral.js#L447
If I do
var price = numeral().unformat(0);
I have an exception from https://github.com/adamwdraper/Numeral-js/blob/master/numeral.js#L79 because inputString is undefined.
How can I unformat when my value is 0 ?
use grunt to concat all js files
from http://numeraljs.com:
var string = numeral().unformat('($10,000.00)');
// '-10000'
It is not a string. The variable name and quotes in '-10000'
are misleading.
In the dropdown to select a language it says Portogues (Portugual), it should say Portugal without the second u
Loving Numeral.js! But I noticed this issue. The initial format of an entered value works just fine. But if you go back in to edit the value, it will reset to 0. The only time it doesn't reset to 0 is when you add digits to the front of the number.
0 -> input 1000
// 1,000
1,000 -> edit 1,010
// 0
Fix? Important when dealing with large values, typing the whole value back in each time is counter-intuitive and opens the opportunity for user typos / error. It doesn't like copy-paste either, but I suspect it's one and the same issue.
Thanks!
It would be nice if there is an option to not display zero values.
numeral(1).format('0[.]0') will print '1' if the decimal delimiter is '.', but '1,0' if the decimal delimiter is ',' (e.g. Spanish).
I'm working on a pull request (new to github) with a fix and tests.
We are trying to format the negative curecny for nl-NL . So the negative pattern is ' $ n- ' .
So I have formatted the currecny as numeral("-102300.00").format("$10,00.00-")
but its giving wrong results . Am i doing some thing wrong how can I achieve this.
Thanks in Advance .
Hello
I saw that you provide a value() method in order to retrieve the native value of the number.
Why not using the valueOf() method to do that, just like momentjs does? It'll make us able to use the object in comparisons such as obj > 1, and is better for semantic since it is a native method.
The implementation should be exactly like the value method:
value : function () {
return this._n;
},
valueOf : function () {
return this._n;
},
we can no longer use component to install your library
In some languages the currency symbol is after the number (price). Are you planing this feature ?
Hi,
I'm trying to create a webjar (http://www.webjars.org/) for numeral-js
But for now I can only download the lib via https://github.com/adamwdraper/Numeral-js/zipball/master and it lead me to 2 pbs :
a/ it is the trunk of the lib, and can change without notice
b/ the zip created has the following first level folder : adamwdraper-Numeral-js-15de409
with a tag 1.3.1 it would be adamwdraper-Numeral-js-1.3.1 (handled automatically by github)
which is a lot more friendly (no need to know the hash of the master)
Thx,
It would be nice if there could be option to always have signed value. In example:
numeral(-123.34).format('+0.0') // -> -123.3
numeral(123.34).format('+0.0') // -> +123.3
numeral(-123.34).format('+$0.0') // -> -$123.3
numeral(123.34).format('+$0.0') // -> +$123.3
The hypothetical scenario would be that you have an app that needs to be able to show a cost of something in various languages/currencies (which a ton of e-commerce sites do). It would be great if we could provide that to the developer rather than having them put a bunch of conditionals in their code so they can add the correct format.
It could then also be used in unformat for greater accuracy when working with the various language-specific currency formats — right now it's pretty inflexible.
Maybe similar to L, LL, LLL, and LLLL in Moment.js it would be something like:
$: $100k, € 500k, 500 tis. Kč, etc.
$$: $500,000, € 500.000, 500 000 Kč, etc.
$$$: $500,000.00, € 500.000,00, 500 000,00 Kč, etc.
Something like that — just quickly threw that together so it can obviously change. Feels like that's an area where Numeral-js could be really useful. Any thoughts? (I would be happy to write the code if there's interest.)
When a user subtracts a decimal from another decimal, the output can be inexact.
For example:
var firstNumber = numeral(123.1);
var secondNumber = numeral(123);
var result = firstNumber.subtract(secondNumber);
In this instance, result has the value of 0.09999999999999432 rather than 0.1.
is there any chance to be able to add prefix or suffix in the format?
something like
$ 0[.]00 */pcs
can become to
$ 5.42 /pcs
btw, very useful lib :)
Hi,
Would be great to be able to do something like this:
var value = numeral().unformat(value),
discount = numeral().unformat(discount);
return value.subtract(discount);
Currently, the numeral.fn.unformat
returns a Numeric
instead of a instance of Numeral
.
So I have to do that:
var value = numeral(numeral().unformat(value)),
discount = numeral(numeral().unformat(discount));
return value.subtract(discount);
Makes sense?
var string = numeral(1000).format('(0,0)');
// (1,000)
Currently, Numeral.js opts for throwing an exception when setting to a language that is not defined. For some environments, this may be desirable, for others it is rather unfortunate.
I propose to offer fallbacks (possibly on a different call that language
to avoid changing the interface):
de-AT
is requested, but not found, de
is tried instead; this allows one file for Germany and Austria (and Luxembourg), which are close enough in this case.hm
is requested and not found, fall back to C
which is some default that poduces sensible-enough formatting, probably exactly what plain JavaScript would do or perhaps en
3.7504E7 Should be unformat to 37,504,000 rather than 3.7504 or other values
Is there any way to fix it quickly? Thanks
Was just talking to a friend about this and in parts of India (or maybe everywhere in India) they format numbers like so: 1,00,00,000 (separator after hundreds until the last thousand) which right now, I believe, is impossible to re-create. There are probably other languages too that format their numbers like this or even more differently.
This is probably a substantial change to how Numeral-js does things. To solve, I assume Numeral-js would need to allow functions instead of objects for the "separator" property of a language which would then allow people to write their own custom formatters. The other possibility is allowing them to specify a custom format like so: '00,00,00,00,00,00,000.00' or something, but that seems kind of unwieldy since you have to make it arbitrarily big to make sure it always works.
I want to show cents if there are any, otherwise I just want to display the integer alone:
var format = (input%100 == 0) ? '$0,0' : '$0,0.00'
Does Numeral have any tricks up its sleeve for this use case?
When you unformat "1st", it results in a trillion, because the 't' is recognised. While it should be 1.
It shouldn't be that hard to implement. I took your numeral.js files and added some options.
numeral.delimiters = {
thousands: ',',
decimals: '.'
}
For french language, it should be something like that:
numeral.delimiters = {
thousands: ' ',
decimals: ','
}
Replace line 179 by
w = w.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1' + numeral.delimiters.thousands);
Replace line 188 by
d = numeral.delimiters.decimals + toFixed(n._n, precision.length).split('.')[1];
This is a quick fix to add different kind of delimiters. If you're interested to make that lib a bit more complete. I suggest you to take a look at babel.numbers
http://babel.edgewall.org/wiki/ApiDocs/babel.numbers
Having something similar to that in javascript would be simply awesome.
This ISO standard defines country codes in capitals:
http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
Also this standard defines language tags in the form de-AT or en-GB etc.
http://en.wikipedia.org/wiki/IETF_language_tag
Same point I made here for momen.js:
Currently, the lang files mix upper and lower case for the variant suffix which is not good I think.
numeral(1073741825).format('0b');
//"1GB"
numeral(1073741823).format('0b');
//"1024MB"
numeral(1073741824).format('0b');
//"1073741824"
Hi,
Thanks for your work.
What about support for 10^9 (billion), 10^12 (milliard), 10^15 (trillion) abbreviations?
Best regards.
Currently the docs say nothing about rounding, requiring experimentation (or browsing the source) to see what's going on.
I believe you should add bytes suffix (MB,KB,...) to the language definition.
For example in french, KB = Ko (because "byte" = "octet").
If you do this I can pull request the french modifications.
Currently there isn't anyway to easily pad a number. For instance if I want my percentages to be at least 3 numbers, so that they are all roughly the same length, there isn't a way to do that easily. '0.0%'
will result in xx.x% and x.x%.
numeral("1.9966").format("0.00")
v 1.2.2
Hey, can you for a git tag for 1.4.9? This helps when Bower users do
bower install numeral
Really appreciate your work! Thanks!
I get this error when trying to do numeral.unformat('R1,000,000');
Uncaught TypeError: Object function (input) {
if (numeral.isNumeral(input)) {
input = input.value();
} else if (!Number(input)) {
input = 0;
}
return new Numeral(Number(input));
} has no method 'unformat'
I'm using requirejs
Using a format specifier with '$' and '+' produces inconsistent behavior:
numeral(-1000).format('$+0,0') === '-$1,000'
numeral(1000).format('$+0,0') === '$+1,000'
It would be great if numeraljs would allow to validate a given string to be a number (or not).
I was wondering if this would be possible by using a similar approach like unformat(), removing thousand separator, the abbreviations first, then taking the result of
string.replace(/[^0-9.-]+/g, '')
and determining if the length is different to the length of the source string (--> check if the string contained invalid characters).
What do you think?
When I try to format 1.96 or a similar number (1.9673897493872), and format it to 1 decimal place, I get 1.0 instead of the expected 1.9
> numeral(1.96).format("0.0")
< "1.0"
numeral(0.4).format()
TypeError: Cannot call method 'indexOf' of undefined
Should assume something like '0' in the same way moment.format()
uses a sensible default.
I would expect .unformat()
to return 0
when passed an empty string.
Hello,
numeral should expose the languages collection. This way the language definition is available.
In my case I'm removing the thousands delimiter and currency symbol from an input text and test if it's a number.
Example
var langData = numeral.languages()[numeral.language()];
var re = new RegExp('[' + langData.delimiters.thousands + langData.currency.symbol + ']', "g");
var rawValue = value.replace(re, '');
var isNumber = Number(rawValue);
Thanks
This has probably not been changed as I'm expecting most are used to it defaulting to this behavior, but in my opinion, numeral('not a number') should definitely not evaluate to zero. Is there a particular reason that the top level parser sets something to a number right after ascertaining that it isn't numeric?
I'm not expecting the default behavior to change for this, but couldn't there be some sort of "tolerant" setting that can be optionally disabled?
please find here the italian language for numeral.js
// numeral.js language configuration
// language : italian Italy (it)
// author : Giacomo Trombi : http://cinquepunti.it
(function () {
var language = {
delimiters: {
thousands: '.',
decimal: ','
},
abbreviations: {
thousand: 'mila',
million: 'mil'
},
ordinal: function (number) {
var b = number % 10;
return '°';
},
currency: {
symbol: '€'
}
};
// Node
if (typeof module !== 'undefined' && module.exports) {
module.exports = language;
}
// Browser
if (typeof window !== 'undefined' && this.numeral && this.numeral.language) {
this.numeral.language('it', language);
}
}());
support formats like:
$ 0.00
0.00 $
0 b
0 a
Would be great if there was a distinction between the two as per current standards.
At the moment, we have be-nl
(where the country comes first) and en-gb
(where the country comes second). This is confusing.
I propose adhering to some well-defined standard, cf. Wikipedia or what the HTTP Accept-Language header uses (BCP-47). The language comes first, the country is second and in uppercase. The languages from above would be nl-BE
and en-GB
.
I'd like to know how you might suggest implementing Numeral in an application which needs to display both bits and bytes, as well as requiring decimal and binary prefixed versions for each.
I'm happy to work towards a pull request with such functionality, but given the way the API is put together at present, I can't see any clean way to specify multiple behaviours, or even just parameters.
I assume this is a regression:
var n = numeral(12345.6)
n.format('0b')
"12KB"
n.format('0b')
"12B"
n.value()
12.05625
Hi,
Would be great to return a new numeral instance on math operations like this:
var value = numeral(100),
discount = numeral(0);
return value.subtract(value.multiply(discount).divide(numeral(100)));
It will return 0
instead of 90
since the value.multiply(discount)
will change the value instance to 0
instead of returning a new instance with 0.
The idea is to mutate the numeral instance only using numeral.fn.set
instead of mutating the called instance.
WDYT?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.