Trying to format a number to two decimals format in European culture. So that comma is decimal separator and space thousands separator.
In example 213245 should be formatted as 213 245,00
How can I do that?
213245.toFixed(2).toLocaleString();
gives 213245.00 but it should be 213 245,00
however
213245.toLocaleString()
gives 213 245
Fiddling below:
var out, input;
input = 213245;
// TEST 1
out = input.toFixed(2);
console.log(out); // 213245.00
out = out.toLocaleString();
console.log(out); // 213245.00
// TEST 2
out = input.toLocaleString();
console.log(out); // 213 245
https://jsfiddle.net/rootnode/8p2vad8u/7/
When you use Number.toFixed() you obtain a string (not a number any more). For that reason, subsequent calls to .toLocaleString() launch the generic Object.toLocaleString() method that knows nothing about numbers, instead of the Number.toLocaleString() you want.
Having a look at the documentation we can compose something like this:
> Number(213245).toLocaleString("es-ES", {minimumFractionDigits: 2});
"213.245,00"
console.log(Number(213245).toLocaleString("es-ES", {minimumFractionDigits: 2}));
This is a relatively new addition so make sure to verify browser support, but it's been working in Firefox and Chrome-like browsers for a few years now. In particular, some runtimes like Node.js do not include the full ICU dataset by default.
Related
In javascript, i'm trying to use parseFloat on string. My string is "22." so when i'm parsing it to float it is converting to 22
so is there any flexibility where i can get the float value as 22.
i want it in float. I searched and got a function "tofixed()" where i can get it as 22.0 but i want it as 22.
var x="22.";
var y = parseFloat(x);
print y // prints 22
But i want it to be 22.
I want it with a "." appended in the end
tried using parseFloat()
I expect the output of parseFloat to be 22, but the actual output is 22
Note that JavaScript does not distinguish between integers and floats. Both (e.g.) 22 and 22.5 are of type number. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures.
So if you want the output to look a certain way when printed to the console, it's probably best to manually convert it into a string you're happy with.
Exactly what to do depends on things like what kind of behavior you want when there is a non-integer component to the number. To get started, maybe try something like:
var x = 22;
console.log(x.toFixed(1));
// Prints 22.0
It has an extra "0" versus your desired output above, but you could always remove that.
What way i can convert string with 16 digits and 2 fraction value to number ?
Currently when I try to convert Number('1234567890123456.12') will became to 1234567890123456. fraction values will be gone.
I just want to confirm without using any third party lib can i convert this string to number ?
Unfortunately not. Javascript represents it's numbers using double precision floating point numbers. At 16 digits, it will only be able to store the integer component and not the part after the decimal point. You will need a bignum library to use this value.
EDIT: for reference the biggest integer you can use in JavaScript is 9,007,199,254,740,991
EDIT2: Thanks to Jeremy you can use a library like bignumberJS.
Your number has too many algorisms, I've created an example that simulates in the first position of the array the maximum length possible in javascript.
var nums = [
"12345678910111.12",
"1.5323",
"-42.7789"
];
nums.forEach(function(n) {
console.log(parseFloat(n).toFixed(2));
});
https://jsfiddle.net/7zzz1qzt/
I have faced issue to convert 18 digit string number to number. It is convert all digit to 0 after 16 digit. I have apply below code. it is working fine for me.
[{"id":${id},"name":"${name}"}]
Good day,
In my java action file, I have a list:
protected ArrayList< String > deletedCorpRegisteredAccountRefNo = new ArrayList< String >( );
And then I put in some data inside this ArrayList, and print it out:
System.out.println( "deletedCorpRegisteredAccountRefNo : "+
deletedCorpRegisteredAccountRefNo );
And the result display correctly:
deletedCorpRegisteredAccountRefNo : [0000000000000234324, 0000000000015422629]
Then I go to my jsp and my javaScript function, I tried to assign this ArrayList value to a var call refNoList, and print out:
var refNoList = ${actionBean.deletedCorpRegisteredAccountRefNo};
console.log(refNoList);
I am expected I will see my browser console will print out something like what I see in Java System.out.println(), but I get another result:
[80084, 15422629]
The 15422629 is still acceptable, because maybe JavaScript auto trim the 0. But not understand why 0000000000000234324 will become 80084, this is totally different.
I try to google to find what is the root cause, but fail to get it, I think I am asking the wrong question in Google.
Kindly advise.
If a JavaScript number starts with zero, it can be interpreted as an octal value. And
var x = 0234324;
console.log(x);
this writes out 80084, because in decimal, this is the same value, as 234324 in octal.
You need to trim the trailing zeroes from the strings, and it will work as you want it to.
Other number definitions
In JavaScript the numbers can be interpreted as binary, octal, decimal or hexadecimal. Here a little list of different literals:
123 - normal decimal
0123 - octal: decimal value is 83
0x123 - hexadecimal: decimal value is 291
0o123 - octal: decimal value is 123
0b101 - binary: decimal value is 5
The problem with the second example (0123) is that it is only interpreted as octal when it is possible. E.g. 09123 and 081 will be interpreted as decimal. Moral of the story, you should not depend on this behaviour, if you need an octal, use 0o explicitely, if you need decimals, trim those zeroes.
Fixing the issue
To parse the number as a decimal, no matter what, you can simply give the radix value as a parameter to parseInt(number, radix):
var x = "000000123";
console.log(parseInt(x, 10)); // prints out 123
I get the value 17.000, 17 thousands.
var pay = document.getElementbyId("name").InnerHTML;
alert(pay);
it shows "17.000". But if I do like this:
var f = (pay * 24);
alert(f);
it shows 408, not 408.000. It makes 17 * 24 not 17.000 * 24. How to work around this?
The period is the decimal point in the English language. If you want "17.000" to be treated as seventeen-thousand and not seventeen, you have to remove the period:
var pay = +document.getElementById("name").innerHTML.replace(/\./g, '');
The unary plus (+) at the beginning converts the resulting string into a number. This is not strictly necessary in your example but can avoid problems in the long run.
Using parseFloat exposes the same problem as implicit type conversion.
If you want to format the number (i.e. convert it to a string with thousand separators) again, have a look at How to print a number with commas as thousands separators in JavaScript. Just use a period instead of a comma.
I've got an object containing dates in the format YYYY-MM-DD.
I'm extracting the various year, month and day integers so I can send them to a different API.
Here's an example of my method, using substr()
Demo: http://jsfiddle.net/AppSynergy/tELsw/
OK, it works. OH NO! - it doesn't - not quite.. What's wrong with the 3rd element, where the "08" in 8th April decides to be 0 instead?
If I change "08" to another integer e.g. "03", it's fine. But "08" causes issues..
This one is driving me crazy -- what's wrong?
If you can spot it, you deserve ice-cream.
08 is considered as an (invalid) octal literal by default.
You have to explicitly specify the radix in your call to parseInt() in order for this token to be considered as a decimal (base 10) number:
$("#debug").append(parseInt(date.substr(5, 2), 10) + " / ");
You need to use parseInt with radix/base of 10 since preceding 0 converts your number in octal notation.
parseInt(yourNum, 10);
Because of that reason, it turns out that you should ALWAYS specify base when using parseInt unless you are not working on base 10 numbers.
If string begins with "0", javascript will think the radix is eight (octal).
You need to tell javascript to parse the string by base of 10.
$.each(testData, function(i, val) {
// sort out the date format
var date = val.trim();
$('#debug').append(date+' ==><br />');
$('#debug').append(parseInt(date.substr(0, 4), 10)+' / ');
$('#debug').append(parseInt(date.substr(5, 2), 10)+' / ');
$('#debug').append(parseInt(date.substr(8, 2), 10)+'<br /><br />');
});