Retain trailing zeros when using .toFixed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2024 08:58 AM
I want to show a percentage value with two decimal places. Using toFixed(2);
Although working, it truncates the trailing zero (e.g. 93.10% becomes 93.1%).
Is there any way to retain the truncated zero?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2024 09:01 AM
Hi,
Can you show the script you are using?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2024 09:08 AM
Hi @Anurag Tripathi,
I'm calculating the value and storing in a variable, then applying toFixed.
percentCompleteGA = (inactiveGARows / totalGARows) * 100;
percentCompleteGA.toFixed(2)
It calculates correctly and displays correctly 99% of the time. Except when the second decimal is a zero, then it's truncated.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2024 09:15 AM
if totalGARows is 0
Then you will get NaN. Is that right.
In that case I would suggest you add a check to see if totalGARows is 0 then tjust throw 0.00 without calculation.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2024 11:07 AM
Hi @Anurag Tripathi,
I don't think you're understanding the issue.
When the value returned has a trailing zero i.e. 93.10 - then it displays as 93.1 - I would like it to display as 93.10