# String question

**URL:** <https://forum.kirupa.com/t/string-question/106505>\
**Category:** Uncategorized\
**Created:** [April 6, 2004, 3:32pm UTC](https://forum.kirupa.com/t/string-question/106505 "2004-04-06T15:32:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![macneilslt](https://avatars.discourse-cdn.com/v4/letter/m/aeb1de/32.png) [@macneilslt](https://forum.kirupa.com/u/macneilslt)\
**Post date:** [April 6, 2004, 3:32pm UTC](https://forum.kirupa.com/t/string-question/106505/1 "2004-04-06T15:32:00Z")

</div>

I think this has to do with string anyways, or length…

Say i have a function that calculates a nubmer, it outputs a dollar value, so i  
lets say the output should be $1.40 , it would output $1.4, that looks a bit funny, so is there a way to check the length after the decimal, and if it’s only 1 char long, to add a 0?

---

<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:** [April 6, 2004, 4:01pm UTC](https://forum.kirupa.com/t/string-question/106505/2 "2004-04-06T16:01:35Z")

</div>

Try this:

```auto
Number.prototype.toFixed = function (n) {
        var s = Math.round(this * Math.pow(10, n)).toString();
        return (s.substr(0, s.length - n)+"."+s.substr(s.length - n));
}

```

---

<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:** [April 6, 2004, 4:31pm UTC](https://forum.kirupa.com/t/string-question/106505/3 "2004-04-06T16:31:10Z")

</div>

> [@Ilyas da Pom](#):
>
> Try this:
> 
> ```auto
> Number.prototype.toFixed = function (n) {
> var s = Math.round(this * Math.pow(10, n)).toString();
> return (s.substr(0, s.length - n)+"."+s.substr(s.length - n));
> }
> 
> ```

how would i work that into this:

```auto

	penT.restrict = "0-9";
	nicT.restrict = "0-9";
	dimT.restrict = "0-9";
	quaT.restrict = "0-9";

button1.onPress = function() {
	
	
	var pT = pennies.length ? pennies*.01 : _root.penT.text = 0;
	var nT = nickles.length ? nickles*.05 : _root.nicT.text = 0;;
	var dT = dimes.length ? dimes*.1 : _root.dimT.text = 0;
	var qT = quarters.length ? quarters*.25 : _root.quaT.text = 0;
	var tT = pT+nT+dT+qT
	
    
	total.text = "total: $"+tT;
	
}

```

i tried

```auto

	penT.restrict = "0-9";
	nicT.restrict = "0-9";
	dimT.restrict = "0-9";
	quaT.restrict = "0-9";

button1.onPress = function() {
	
	
	var pT = pennies.length ? pennies*.01 : _root.penT.text = 0;
	var nT = nickles.length ? nickles*.05 : _root.nicT.text = 0;;
	var dT = dimes.length ? dimes*.1 : _root.dimT.text = 0;
	var qT = quarters.length ? quarters*.25 : _root.quaT.text = 0;
	var tT = pT+nT+dT+qT
	
    var s = Math.round(tT * Math.pow(10, n)).toString();
    return (s.substr(0, s.length - n)+"."+s.substr(s.length - n));
    
	total.text = "total: $"+s;
	
}

```

but that is incorrect.

---

<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:** [April 6, 2004, 6:49pm UTC](https://forum.kirupa.com/t/string-question/106505/4 "2004-04-06T18:49:24Z")

</div>

```auto

	penT.restrict = "0-9";
	nicT.restrict = "0-9";
	dimT.restrict = "0-9";
	quaT.restrict = "0-9";

button1.onPress = function() {
	
	
	var pT = pennies.length ? pennies*.01 : _root.penT.text = 0;
	var nT = nickles.length ? nickles*.05 : _root.nicT.text = 0;;
	var dT = dimes.length ? dimes*.1 : _root.dimT.text = 0;
	var qT = quarters.length ? quarters*.25 : _root.quaT.text = 0;
	var tT = pT+nT+dT+qT
	n=2
    var s = Math.round(tT * Math.pow(10, n)).toString();
    s= (s.substr(0, s.length - n)+"."+s.substr(s.length - n));
    
	total.text = "total: $"+s;
	
}

```

---

<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:** [April 6, 2004, 7:25pm UTC](https://forum.kirupa.com/t/string-question/106505/5 "2004-04-06T19:25:21Z")

</div>

thanks, i see what i did wrong 🙂

---

<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:** [April 7, 2004, 10:39am UTC](https://forum.kirupa.com/t/string-question/106505/6 "2004-04-07T10:39:00Z")

</div>

You should take advantage of the fact it’s a prototype…

```auto
Number.prototype.toFixed = function (n) {
        var s = Math.round(this * Math.pow(10, n)).toString();
        return (s.substr(0, s.length - n)+"."+s.substr(s.length - n));
}

	penT.restrict = "0-9";
	nicT.restrict = "0-9";
	dimT.restrict = "0-9";
	quaT.restrict = "0-9";

button1.onPress = function() {
	
	
	var pT = pennies.length ? pennies*.01 : _root.penT.text = 0;
	var nT = nickles.length ? nickles*.05 : _root.nicT.text = 0;;
	var dT = dimes.length ? dimes*.1 : _root.dimT.text = 0;
	var qT = quarters.length ? quarters*.25 : _root.quaT.text = 0;
	var tT = pT+nT+dT+qT
    
	total.text = "total: $"+tT.toFixed(2);
}

```
