I am trying to use Intl NumberFormat to represent Nepal's number format. I am a Nepali and I know how we use number in Nepali. It's exactly similar to Hindi language.
1,23,343.32
From last, the comma used after 3 digits and then 2 digits on next occurrence.
The intl number format works with Indian:
let number = 123456.789
console.log(new Intl.NumberFormat('en-IN').format(number));
But it doesn't work with Nepali:
let number = 123456.789
console.log(new Intl.NumberFormat('en-NP').format(number));
After digging deeper, I found it's not ECMAScript issue. ECMAScript uses IANA number and I found no form to submit a bug or issue or complaint and also I found IANA uses ICANN number and there too I couldn't find where I can submit a bug or issue...
I know I can use en-IN to format it in Nepali as well. But I think this is overrule for me. And I know SO is not for asking where to complaint about something, but it's related to JavaScript number format issue somehow and asking here. If anyone can point out where I can submit an issue would be helpful.
Related
I have some logic within a function that takes a string of numbers called digits like so:
6145390195186705543
I then attempt to convert with parseInt() like so:
parseInt(digits)
The result of:
digits = parseInt(digits);
is
6145390195186705000
Can someone help me understand why this is the case? and how i can get an accurate conversion?
This is another version of "broken" floating point math: Javascript uses 64 bits to store numbers as small as the size of an atom up to the number of atoms in the universe. As that is quite a broad range it cannot be stored accurately, therefore the numbers are stored in an imprecise way but can represent a very broad range. In your case 6145390195186705000 is the inaccurate version JS is able to store as 6145390195186705543 cannot be stored.
and how i can get an accurate conversion?
You cannot store an "accurate number", therefore you cannot convert it accurately. However there are some libraries that allow you to work with strings as if they were numbers, such as BigIntJS.
As this is a common problem, this is going to be solved in the next JS version, see this proposal. You can currently test it in the new version of chrome.
I am trying to write a regular expression using javascript in order to validate data entry on an online database. I have attached a screenshot (Screenshot) of the page which is asking me to do this. The question I am asking is what is the weight? The validation I am trying to create is that it needs to be more than 24 kg. I am complete novice at JS (I have good experience with SQL) and have been reading developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions#Writing_a_Regular_Expression_Pattern and
www.w3schools.com/js/js_regexp.asp
I have tried a few things and tested them on rubular but to no avail.
Any advice would be much appreciated and in the meantime I will continue reading.
Many thanks,
Jenna
First off, this sounds the sort of situation for which a regular expression is not a reasonable choice for validation. Just comparing numbers would make much more sense.
However, a regular expression that would match a number greater than or equal to 24 would be:
/^(\d{3,}|[3-9]\d|2[4-9])$/
That is, it matches one of:
\d{3,} a number at least 3 digits long
[3-9]\d a number in the range 30 - 99
2[4-9] a number in the range 24 - 29
To match only greater than 24, just change the last alternative:
/^(\d{3,}|[3-9]\d|2[5-9])$/
This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 3 years ago.
I am getting date from code in "MM/dd/yy" format. I am creating an object of Date in JavaScript as:
var tempDate = new Date(dateStringInMM/dd/yy)
Now, in IE, tempDate.getYear() is giving me "13" and tempDate.getFullYear() is giving "1913" as value.
Is there any provision to get "2013" for IE in JavaScript?
1913 is a perfectly good guess when given only a two-digit date. The browser has no way of knowing what kind of date your site is asking. For example, if you're asking for the user's DOB, 1913 would be a better guess, as someone born in 2013 wouldn't be old enough to use the site yet.
I can see why you might expect it to guess 2013, but given just two digits, there's no way it can reasonably be expected to get it right every time. It's going to be wrong for someone.
The actual reason for this behaviour is likely to be for backward compatibility.
Older browser versions would have guessed 19xx when given a two digit date. It would have been a perfectly legitimate guess back in the day.
In the meanwhile, sites would have been written by authors who knew this and may have done tricks like adding 100 to the value to work around the issue.
This means that if a new version of the browser is released that changes this behaviour, it would break those old sites.
Microsoft in particular tends to be very conservative about changing existing behaviour, because of this kind of thing. Therefore they would have left it working the way it always did.
The real solution here is to not use two-digit dates. Just specify four digits in the first place, and there won't be any problems.
However, that's not the complete picture, because part of the problem is down to the lack of cross-browser consistency with the Date class. You may find that giving two browsers the same date string gives you different values.
For this reason, I recommend not using the built-in Javascript Date class at all, but instead using one of the libraries that exists to help with this.
The two libraries I can recommend are Date.js and Moment.js.
Both of these libraries will allow you to specify a date, and also specify the format that you're using. They will also both work consistently across all browsers, and will also be more likely to give you the right answer when you specify only two digits for the year (although you're still going to have ambiguity there and possible errors, so I still recommend using four digits).
Hope that helps.
The same functoion is working fine in IE, check fiddle here:
<script>
function myFunction()
{
var d = new Date('11/07/2013');
var x = document.getElementById("demo");
x.innerHTML=d.getFullYear();
}
</script>
<button onclick="myFunction()">Get date</button>
<p id="demo">Click the button to display the full year of todays date.</p>
output is 2013
I'm editing a web based program at the moment. It is used all over the globe. I'm adding a number based field. I want to be able to allow the end user to enter in the number the way they want in their local locale. I see that there is a function called Number.toLocaleString() that will give me what I need. However, I can't seem to find an inverse function.
Take the string "1,000" for example. If my user's locale is en-US, I want it to be interpreted as 1000. If my user's locale is de-DE then it should be interpreted as 1. What is the standard way of doing this in JavaScript?
Javascript has pretty much no locale support beyond just detecting what is set. For that, see the navigator object, and this other question.
If you want locale specific parsing, you'll need to use one of the many libraries out there. Checkout localplanet. It has a formatter for integers and decimals.
I'm using a date picker built in to my device and it returns the date in format YYYY/MM/DD
and returns the time in format HH:MM 24 hours.
The overall returned string is YYYY/MM/DDT/HH/MM
T being the separator.
What is the best way to validate this so that it matches the format YYYY/MM/DD/T/HH/MM
Regex ? Does JavaScript have validation for date/time built it even if i have to split at T so i have the date and time in separate variables?
Whats the best way/algorithm to do this aswell preformance wise?
If i split my varible in to array like...
["YYYY/MM/DD","HH:MM"]
Is there a regex or js function that validates this best? Preformance is key.
Thanks
I wouldn't be worried about performance, you're not going to be doing this hundreds of thousands of times in a tight loop, presumably.
A regex is a good way to do it, and will be easy to write; if you create the regex once and reuse it, it should perform well too.
Assuming your example of the format is a typo and you meant YYYY/MM/DDTHH:MM, a modern JavaScript engine should also support using that string as the argument to new Date(...) provided you change the / to - (which you can readily do via String#replace). That's because it fits the pared-down ISO-8601 format defined as part of ES5. But older engines (IE8 and earlier, for instance) may not support it, you'd have to test on the browsers you intend to support. But if it's really YYYY/MM/DDT/HH/MM, ignore this paragraph.