Format number by selector - javascript

I am trying to format a number by using a selector, both jQuery of JavaScript are fine.
I have a dynamic number that comes back from the API, but that comes back with many decimal place (i.e 3.5847549875398). I want to format it to 2 decimal places (i.e 3.58). Rounding is not necessary, but if there is an example with rounding that would be great!
Here is what I have, but it fails to round:
JSP:
<span class="floatRight distanceNum">
<#= storeInfo.distance #> miles
</span>
JavaScript/jQuery (Within the onLoad function)
$(".distanceNum").toFixed(2);

You need to convert your value from api to float then you should be able to achieve what you want. toFixed function will round your value also.
Here is the working code for you.
$(".distanceNum").text(parseFloat($(".distanceNum").text()).toFixed(2));
Let me know if it worked for you.
Happy Coding.

Related

Why does a number get its last 2 digits turned to 0?

Use
Hi, so I was working on my discord bot with some user ids that I stored in a database, which then I will get back to ping them or give them roles.
Problem
Though here's the problem, in the database they get turned from ex: 533692905387196429 to 533692905387196400. I tried setting that to a String, and it worked, in the database, it's stored fully how it's supposed to be. But, when I get them back from the database and turn them into a number or an integer they get turned back to ex: 533692905387196400.
Tried using
I tried using parseInt(), parseFloat() and Number() but all of them give the same result.
More info
Also if I remove 2 digits for example: 533692905387196429I remove 19 from there, it will give back 533692905387(19)6429 instead of 533692905387196429 and instead of 533692905387196400.
Any help is appreciated!
So, Number.MAX_SAFE_INTEGER is 9007199254740991 (which is 253 - 1). Your number is too large to hold full precision.
If you want a number with that level of digits and precision, you can use BigInt and then you will have to be very careful how you use that BigInt as it cannot be directly used in place of a regular Number type, but it can hold infinite precision and you can do math between two BigInt values.
Whether it's best used as a BigInt or as a String really depends upon what you're doing with it. If these are just some sort of ID that you aren't actually doing numeric operations with, then you can just keep it as a string.
The number is too big, so Javascript doesn't keep full precision!
> 533692905387196429
533692905387196400
You can resolve this by storing them as strings:
> '533692905387196429'
'533692905387196429'
You shouldn't need to do any mathematical operations with Discord IDs so there shouldn't be any issue storing and treating them as strings everywhere.

Adding a whole number and a decimal in Javascript results in removal of the decimal?

I'm sure this is simple, but in my javascript code, I have two numbers. One contains a decimal, and the other doesn't, and I add them together (ie. 7.5 + 5), I am getting a result with NO decimal value.
Do I need to cast each number variable as a double? I know that all numbers are doubles in javascript - which is why I do not understand this behavior...
For instance, I have var answer = week1 + week2;. Does this make sense?
Thanks in advance!
I am sorry for wasting time - turns out I was using parseInt instead of parseFloat to gather the "week" values I spoke about.
Can someone please close this question or delete it? Before the shame consumes me?

d3.format wont keep trailing zero

I'm using d3.round(num,2) to round a number from say, 2.567 to 2.57. My problem is that when I do this, I want 2 decimal numbers of precision to be used at all times/regardless of if it is a zero.
when I have a number like 2.201, I want the display to be 2.20, and instead am coming up with 2.2. Is there any way to format the round function to always include zeroes?
Thanks for any help!
Edit: used num.toFixed(2) and that works, but I am returning d3.format("$")(num.toFixed(2)) and that is only returning $2.3, which is the reason I needed this, I am looking to display a price. If anyone has help to offer with that, it would be great, thank you
Try this:
d3.format("$.3n")(num);

How to round off 9999999999999999 to 9999999999999998

In my case, i am converting a string value of '9999999999999999' to integer using parseFloat(). But it converts to next number of it i.e. 10000000000000000. But i need to convert it to before of that number i.e. 999999999999999998. I have searched for a while in google. But could not get clear idea to implement this.
Try this
document.getElementById("demo").innerHTML=Math.round(9999999999999999-2);
OUTPUT
9999999999999998
This number is too big to represented precisely in JavaScript Number value. So no amount of conversion will give you values reliably/precisly as you want around such range.
I.e. (9999999999999999-1)===(9999999999999999) returns true, but (9999999999999998)===(9999999999999999) returns false.
If you need such high precision in JavaScript (similar to many other languages) you need to use specialized data types (unfortunately there is no "BigInteger" type built in in JavaScript).
You will need to use some external javascript library to work with big numbers like that, cause max number you cant represent without losing presicion in javascript integers is 9007199254740992 (Explanation : What is JavaScript's highest integer value that a Number can go to without losing precision?)
Here you have some link where people discuss about some libraries to use for javascript big numbers.
How to deal with big numbers in javascript

Large number in javascript

I am working on a calculator in javascript, where user can enter the values in textfield and operation will be performed.
Now if user enters a very large value
for example 5345345345353453453453535
it is converted to 5.345345345353453e+24
I am using parsrInt() to convert it to integers. and it gives me 5.
which is wrong .
Can anybody suggest how to solve it?
Integers in javascript are, like every numbers, stored as IEEE754 double precision floats.
So you can only exactly store integers up to 2^51 (the size of the mantissa).
This means you'll have to design another format for dealing with big integers, or to use an existing library like BigInteger.js (Google will suggest a few other ones).
Taken from Mozilla documentation:
Parses a string argument and returns an integer of the specified radix
or base.
Therefore parseInt() is taking your value as a string 5.345345345353453e+24
It is then ignoring any non-integer values and classing this as a decimal (5.345...) and then evaluating this to 5.
As #dystroy has pointed out, if you wish to carry out calculations with these large numbers you'll need to use a custom format, or use a pre-existing javascript library.
Try parseFloat instead of parseInt.
<script type="text/javascript">
var value = parseFloat(5345345345353453453453535);
alert(value);
</script>

Categories

Resources