The following throws up an alert box reading: 211466719468855300
. Why is this happening, even when I try casting it to a String? How can I read it accurately?
HTML:
<a class="delete-link" data-id="211466719468855298">Delete</a>
JS:
$('.delete-link').click(function(e) {
var $item =$(this);
var itemID = String($item.data('id'));
alert(itemID);
});
Fiddle: http://jsfiddle.net/zUbym/1/
We can deal with large numbers in JavaScript using the data type BigInt. Advantages: It can hold numbers of large size. It perform arithmetic operations.
To limit decimal places in JavaScript, use the toFixed() method by specifying the number of decimal places.
you need to include <iomanip> and use the std::setprecision manipulator. To get the level of accuracy you want you will need to use double s rather than float s.
Use the toFixed() method to round a number to 2 decimal places, e.g. const result = num. toFixed(2) . The toFixed method will round and format the number to 2 decimal places.
It's happening for two reasons:
If you get the attribute value with ".attr()" directly, you can avoid the numeric conversion, but you'll have to keep it a string. Alternatively, you could prefix your data values with some non-digit character ("_" or whatever) to keep jQuery from trying to do you a favor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With