# 1.50 instead of 1.5....how?

**URL:** <https://forum.kirupa.com/t/1-50-instead-of-1-5-how/260393>\
**Category:** programming\
**Created:** [May 15, 2008, 4:02am UTC](https://forum.kirupa.com/t/1-50-instead-of-1-5-how/260393 "2008-05-15T04:02:24Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![bluemagica](https://avatars.discourse-cdn.com/v4/letter/b/a4c791/32.png) [@bluemagica](https://forum.kirupa.com/u/bluemagica)\
**Post date:** [May 15, 2008, 4:02am UTC](https://forum.kirupa.com/t/1-50-instead-of-1-5-how/260393/1 "2008-05-15T04:02:24Z")

</div>

while adding floats, how do i get the trailing 0? cause flash just seems to terminate it! and also is there a way to fix the number of digits after the decimal place?

---

<div class="post-metadata">

**Author:** ![Jephz](https://avatars.discourse-cdn.com/v4/letter/j/58956e/32.png) [@Jephz](https://forum.kirupa.com/u/Jephz)\
**Post date:** [May 15, 2008, 5:32am UTC](https://forum.kirupa.com/t/1-50-instead-of-1-5-how/260393/2 "2008-05-15T05:32:02Z")

</div>

aren’t they the same thing?

---

<div class="post-metadata">

**Author:** ![Valaran](https://avatars.discourse-cdn.com/v4/letter/v/ee59a6/32.png) [@Valaran](https://forum.kirupa.com/u/Valaran)\
**Post date:** [May 15, 2008, 6:16am UTC](https://forum.kirupa.com/t/1-50-instead-of-1-5-how/260393/3 "2008-05-15T06:16:15Z")

</div>

> [@Jephz;2326462](#):
>
> aren’t they the same thing?

They are.

---

<div class="post-metadata">

**Author:** ![Sirisian](https://avatars.discourse-cdn.com/v4/letter/s/439d5e/32.png) [@Sirisian](https://forum.kirupa.com/u/Sirisian)\
**Post date:** [May 15, 2008, 6:20am UTC](https://forum.kirupa.com/t/1-50-instead-of-1-5-how/260393/4 "2008-05-15T06:20:35Z")

</div>

Do you know how floating point math works?  
[Floating Point](http://en.wikipedia.org/wiki/Floating_point)

Are you asking about representing a float as a string with x number of decimal digit  
[toFixed()](http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/Number.html#toFixed%28%29)

Also look at toPrecision in case you need it later.

---

<div class="post-metadata">

**Author:** ![bluemagica](https://avatars.discourse-cdn.com/v4/letter/b/a4c791/32.png) [@bluemagica](https://forum.kirupa.com/u/bluemagica)\
**Post date:** [May 15, 2008, 7:54am UTC](https://forum.kirupa.com/t/1-50-instead-of-1-5-how/260393/5 "2008-05-15T07:54:08Z")

</div>

I didnt find those in the help manual sirisian…i am using AS2, can you help in some more detail.

And you all are right, 1.5 is same as 1.50 in math…but you see i am setting up the score system for “money” and in monetary system 1.5(five cent) and 1.50(fifty cent) are different. And this is also the reason i need 2 fixed decimal places in the number.

if i do 1.50+1.50, it will give 3, but i want 3.00…do you guys see my problem now?

---

<div class="post-metadata">

**Author:** ![ArmoredSandwich](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@ArmoredSandwich](https://forum.kirupa.com/u/ArmoredSandwich)\
**Post date:** [May 15, 2008, 8:49am UTC](https://forum.kirupa.com/t/1-50-instead-of-1-5-how/260393/6 "2008-05-15T08:49:54Z")

</div>

[quote=bluemagica;2326495]I didnt find those in the help manual sirisian…i am using AS2, can you help in some more detail.

And you all are right, 1.5 is same as 1.50 in math…but you see i am setting up the score system for “money” and in monetary system 1.5(five cent) and 1.50(fifty cent) are different. And this is also the reason i need 2 fixed decimal places in the number.

if i do 1.50+1.50, it will give 3, but i want 3.00…do you guys see my problem now?[/quote]

If you can’t find a better way, couldn’t you just check for the length and add a zero if necessary?

---

<div class="post-metadata">

**Author:** ![bluemagica](https://avatars.discourse-cdn.com/v4/letter/b/a4c791/32.png) [@bluemagica](https://forum.kirupa.com/u/bluemagica)\
**Post date:** [May 15, 2008, 8:59am UTC](https://forum.kirupa.com/t/1-50-instead-of-1-5-how/260393/7 "2008-05-15T08:59:48Z")

</div>

That is a make shift way, i am looking for some actual way out!

---

<div class="post-metadata">

**Author:** ![Sirisian](https://avatars.discourse-cdn.com/v4/letter/s/439d5e/32.png) [@Sirisian](https://forum.kirupa.com/u/Sirisian)\
**Post date:** [May 15, 2008, 8:38pm UTC](https://forum.kirupa.com/t/1-50-instead-of-1-5-how/260393/8 "2008-05-15T20:38:11Z")

</div>

Hmm, I thought I told everyone to use AS3 a while back. I guess the word didn’t get around. AS2 has no native method for turning a Number into a fixed type.

var n1:Number = 1.5;  
var n2:Number = 1.5;  
var n3:Number = n1 + n2;  
var n1Plusn2:String = (n3 \* 100).toString();  
var indexOfPeriod:int = n1Plusn2.indexOf(".");  
n1Plusn2 = n1Plusn2.substring(0, indexOfPeriod - 1) + “.” + n1Plusn2.substr(indexOfPeriod, 2);

something like that. You might have to test it.

[Here’s the documentation for string.](http://livedocs.adobe.com/flash/8/main/wwhelp/wwhimpl/js/html/wwhelp.htm?href=Part4_ASLR2.html)

Also this isn’t really game programming related. Remember to say the language you are using or people might assume it’s AS3.

---

<div class="post-metadata">

**Author:** ![cooldude88](https://avatars.discourse-cdn.com/v4/letter/c/e0b2c6/32.png) [@cooldude88](https://forum.kirupa.com/u/cooldude88)\
**Post date:** [May 15, 2008, 11:43pm UTC](https://forum.kirupa.com/t/1-50-instead-of-1-5-how/260393/9 "2008-05-15T23:43:27Z")

</div>

cool code sirisian but i would say its safe to say that the chancs of it being as2 is still pretty high so dont assume nothing

---

<div class="post-metadata">

**Author:** ![glosrfc](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/glosrfc/32/8334_2.png) [@glosrfc](https://forum.kirupa.com/u/glosrfc)\
**Post date:** [May 18, 2008, 12:00am UTC](https://forum.kirupa.com/t/1-50-instead-of-1-5-how/260393/10 "2008-05-18T00:00:07Z")

</div>

Maybe a bit late, but these two functions might be useful:

```auto
// turn a number into a currency string 
function toCurr(num:Number):String {
	var currency:String = "£";
	var majUnit:Number = Math.floor(num);
	var minUnit:Number = Math.round((num - majUnit) * 100);
	var majUnitStr:String = String(majUnit);
	var minUnitStr:String;
	minUnit < 10 ? minUnitStr = "0" + String(minUnit) : minUnitStr = String(minUnit);
	return currency + majUnit + "." + minUnitStr;
}
// turn a currency string into a number 
function fromCurr(currency:String):Number {
	var str:String = currency.substr(1);
	return parseFloat(str);
}
//testing - note the subtle difference if you want to perform any maths functions on currency strings
var a:Number = 1.50;
trace("The sum of two numbers is " + (a + a));
trace("The sum of two currency amounts is " + toCurr(a + a));
var b:String = "£1.50";
trace("The sum(!) of two currency strings is " + (b + b));
trace("The sum of two currency amounts is " + (fromCurr(b) + fromCurr(b)));

```

---

<div class="post-metadata">

**Author:** ![Iamthejuggler](https://avatars.discourse-cdn.com/v4/letter/i/ecc23a/32.png) [@Iamthejuggler](https://forum.kirupa.com/u/Iamthejuggler)\
**Post date:** [May 18, 2008, 11:17am UTC](https://forum.kirupa.com/t/1-50-instead-of-1-5-how/260393/11 "2008-05-18T11:17:23Z")

</div>

I would think the easiest way is to have a number that represents currency, and just use cents, don’t bother with dollars/pounds. So to add £1 you actually add 100. Then for the display use a custom toString method that uses the length of the number, and if it is less than three add “£0.” to the start, and if it is greater than three change it to “£” + (all the digits except the last two) + “.” + (the last two digits).

---

<div class="post-metadata">

**Author:** ![bluemagica](https://avatars.discourse-cdn.com/v4/letter/b/a4c791/32.png) [@bluemagica](https://forum.kirupa.com/u/bluemagica)\
**Post date:** [May 18, 2008, 11:42am UTC](https://forum.kirupa.com/t/1-50-instead-of-1-5-how/260393/12 "2008-05-18T11:42:10Z")

</div>

umm guys, thanks for all the help, but i have managed to solve it using a makeshift way! But Still those were really a great help in my learning
