JavaScript's Number.prototype.toFixed formats a number using fixed-point notation.
Questions tagged [tofixed]
160 questions
51
votes
4 answers
.toFixed not for .0*
I have a few values:
var one = 1.0000
var two = 1.1000
var three = 1.1200
var four = 1.1230
and function:
function tofixed(val)
{
return val.toFixed(2);
}
this return:
1.00
1.10
1.12
1.12
LIVE
I want maximum size after dot - 2, but only if…

user2565589
- 543
- 1
- 4
- 6
28
votes
2 answers
Have max. 2 decimal places
With .toFixed(2) I always get 2 decimals, even if the number is 2.00
Can I get "2" instead?
Example:
2.00 => 2
2.05 => 2.05
2.053435 => 2.05
2.057435 => 2.06

Elfy
- 1,733
- 6
- 19
- 39
16
votes
7 answers
javascript - how to prevent toFixed from rounding off decimal numbers
I'm very new to html, javascript, and css so please forgive if my question sounds idiotic to you. My question is how can I prevent the function toFixed() from rounding of the decimal number.
Here's my link: http://jsfiddle.net/RWBaA/4/
What I'm…

TheOnlyIdiot
- 1,182
- 7
- 17
- 37
15
votes
3 answers
Typescript, toFixed. Type 'string' is not assignable to type 'number'
distanceX = ((this.frontclickedX - originX) / originX).toFixed(2);
distanceY = (-(this.frontclickedY - originY) / originY).toFixed(2);
let distanceZ: number = (-(this.frontclickedY - originY) / originY).toFixed(2);
my browser…

Yoran Cobben
- 153
- 1
- 1
- 6
14
votes
6 answers
Javascript toFixed function
The expected result of:
(1.175).toFixed(2) = 1.18 and
(5.175).toFixed(2) = 5.18
But in JS showing:
(1.175).toFixed(2) = 1.18 but
*(5.175).toFixed(2) = 5.17*
How to rectify the problem?

Sushovan Mukherjee
- 594
- 5
- 13
9
votes
4 answers
JS- toFixed returns a string, but I need a number to 6 digits
I am working with another codebase that I have no control over, and that I cannot see. I can send it inputs and it will return outputs to me. This codebase also doesn't specify what it is written in, but that doesn't matter since I only need to pass…

Michael Treat
- 359
- 1
- 4
- 10
6
votes
4 answers
Using toFloat and setState on number input in React
I have a number input field that sets the new value through the state when the user is typing. This works fine. If I however add decimals to it (toFixed), the cursor will jump to the end of the input after typing only one digit. This happens when I,…

Ellinor
- 323
- 3
- 14
5
votes
2 answers
Display floating point with starting values in DAT.GUI
I have a small menu made with dat.gui JavaScript library. I use different lines with some initial values which are displayed (these initial values are modified during the execution of code).
My issue is, for example, that instead of display a value…
user1773603
5
votes
7 answers
Reliable JS rounding numbers with toFixed(2) of a 3 decimal number
I am simply trying to round up 1.275.toFixed(2) and I was expecting a return of 1.28, rather than 1.27.
Using various calculators and the simple method of rounding to the nearest hundredth, if the last digit is greater than or equal to five, it…

jQuerybeast
- 14,130
- 38
- 118
- 196
5
votes
2 answers
.toFixed() called on negative exponential numbers returns a number, not a string
I noticed that when calling toFixed against a negative exponential number, the result is a number, not a string.
First, let's take a look at specs.
Number.prototype.toFixed (fractionDigits)
Return a String containing this Number value represented…

Ionică Bizău
- 109,027
- 88
- 289
- 474
5
votes
1 answer
Mustache.js lambdas and number formatting toFixed
I'm trying to set up a mustache.js template that formats a number to a specific decimal place using a lambda and I'm running into issues. Given an object that looks like:
{
x: 123,
points: [
{ name: "foo", y: 1.234567 },
…

roto
- 677
- 6
- 11
4
votes
1 answer
Does a hardcode float num a.bc... (0<=n<100 decimals,MIN_VALUE<=a.bc...<=MAX_VALUE) always equals to Number.parseFloat((a.bc...).toFixed(n))?
For example, I know 0.12,ie:
const a=0.12;
rounds to something due to float number errors. However, I'm not asking why the error happens and I don't care the errors currently, instead I want to know if the hardcode form always equals to the value…

displaydisplayname
- 217
- 1
- 5
4
votes
1 answer
number_format() in php vs toFixed() in jQuery for validation
I am having some data calculation on my client side, which I want to validate on the server end. Let's say my calculated value result is 34.55
(34.55).toFixed(1) gives me 34.5
Strangely, (34.555).toFixed(1) gives me 34.6
number_format(34.55, 1, '.',…

Annapurna
- 529
- 1
- 6
- 19
4
votes
4 answers
JavaScript - converting 3 numbers to percentage, doesn't yield 100% total
I have a scenario where I have three numbers:
17
10
90
I need to convert those into whole percentage values (so that when added, total 100% as you'd expect). I have this function:
function roundPercentageTotals(num1, num2, num3) {
var…

Rob
- 6,819
- 17
- 71
- 131
4
votes
1 answer
In Javascript, is toFixed() accurate enough for currency?
I am creating a script for calculating an order total. There are certain variables that can change the price and therefore some long-digit decimals will occur.
Is toFixed() precise enough to round these numbers and always get the same…

twiz
- 9,041
- 8
- 52
- 84