# Fixed decimals

**URL:** <https://forum.kirupa.com/t/fixed-decimals/102272>\
**Category:** Uncategorized\
**Created:** [February 21, 2004, 4:37pm UTC](https://forum.kirupa.com/t/fixed-decimals/102272 "2004-02-21T16:37:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hommer](https://avatars.discourse-cdn.com/v4/letter/h/a3d4f5/32.png) [@Hommer](https://forum.kirupa.com/u/Hommer)\
**Post date:** [February 21, 2004, 4:37pm UTC](https://forum.kirupa.com/t/fixed-decimals/102272/1 "2004-02-21T16:37:21Z")

</div>

Arh! I’ve forgotten how to make decimal variables fixed.  
I’m making a small game with a timer that have to show the time in seconds with 2 decimals all the time (i.e. 34.71 sec. or 51.00 sec.)  
How do I force the time-variable to always have two decimals?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 21, 2004, 5:06pm UTC](https://forum.kirupa.com/t/fixed-decimals/102272/2 "2004-02-21T17:06:23Z")

</div>

If you had searched you’d have found you answer in no time.  
[http://www.kirupaforum.com/forums/showthread.php?s=&threadid=43439&](http://www.kirupaforum.com/forums/showthread.php?s=&threadid=43439&)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 21, 2004, 5:19pm UTC](https://forum.kirupa.com/t/fixed-decimals/102272/3 "2004-02-21T17:19:10Z")

</div>

The only way I know is this:

Let’s say your un-trimmed variable is 123456789. What you do is you multiply it with 0.00001 so that you get 1234.56789.  
Then you use Math.round() on the var so the result will be 1235. Then you multiply it with 0.01, returning 12.35.

[AS]  
yourVar = 123456789;  
yourVar \*= 0.00001; //returns 1234.56789  
yourVar = Math.round(yourVar); // returns 1235  
yourVar \*= 0.01  
trace(yourVar); // returns 12.35  
[/AS]

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 21, 2004, 8:12pm UTC](https://forum.kirupa.com/t/fixed-decimals/102272/4 "2004-02-21T20:12:48Z")

</div>

well the problem with that is if it’s 1200, and if u mulitply it by 0.01 u’ll get 12 and not 12.00.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 21, 2004, 8:54pm UTC](https://forum.kirupa.com/t/fixed-decimals/102272/5 "2004-02-21T20:54:10Z")

</div>

> _Originally posted by davidchang_  
> **well the problem with that is if it’s 1200, and if u mulitply it by 0.01 u’ll get 12 and not 12.00.**

You can always convert the number into a string and add some formatting to it. 😉

```auto
function getSeconds(ms) {
	var s = String(Math.round((ms/1000)*100)/100);
	var n = (s.indexOf(".")<0 ? s+"." : s).split(".");
	while (n[1].length<2) n[1] = n[1]+"0";
	return n[0]+"."+n[1];
}
trace(getSeconds(0)); //0.00
trace(getSeconds(10000)); //10.00
trace(getSeconds(10007)); //10.01
trace(getSeconds(10070)); //10.07
trace(getSeconds(10700)); //10.70

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 21, 2004, 8:58pm UTC](https://forum.kirupa.com/t/fixed-decimals/102272/6 "2004-02-21T20:58:17Z")

</div>

[AS]function decimal(number, decimalPlace){  
var t = “” + Math.round(number \* Math.pow(10, decimalPlace)) / Math.pow(10, decimalPlace);

```
if (t.indexOf(".") == -1) {
	t += ".";
}

while(t.length - t.indexOf(".") &lt;= decimalPlace) {
	t += "0";
}	

//Note: the output will be a String and not a numerical value.
return t;

```

}[/AS]
