Aidan
Aidan

Reputation: 13

Function to convert numbers over 1000 to 1k etc. AS3

G'day,

The function is coded, but it's on the stage frame. I'm looking to get it converted into a more dynamic function so I can just call it on all my textfields.

Here's the code:

function numtolet():void
{
    output.text = String(int(earner  * 100) / 100);
    if (earner >= 1000 && earner < 1000000)
    {
        output.text = String(int((earner/1000)  * 100) / 100 + "k");
    }
    else if (earner >=1000000 && earner < 1000000000)
    {
        output.text = String(int((earner/ 1000000) * 100 ) / 100 + " M");
    }
}

I'm looking to turn the 'output.text' portion into a variable that changes based on the text field calling the function and 'earner' to the variable the textfield reads.

Cheers,

-Aidan.

Upvotes: 0

Views: 576

Answers (4)

jdfinch3
jdfinch3

Reputation: 513

Here's a quick function I wrote up for something else. It can easily be adapted to larger numbers by adding another if statement and adding 000 to the number. It also doesn't include the output display, but that can very easily be added as well. Hope it helps!

Call the function via numToLet(earner).

     function numToLet(x) {
        if (x > 1000000000000000000) {
            x = x / 1000000000000000000
            x = Number(x.toFixed(2));
            return x + "Quin"; 
        }
        if (x > 1000000000000000) {
            x = x / 1000000000000000
            x = Number(x.toFixed(2));
            return x + "Quad"; 
        }
        if (x > 1000000000000) {
            x = x / 1000000000000
            x = Number(x.toFixed(2));
            return x + "Tril"; 
        }
        if (x > 1000000000) {
            x = x / 1000000000
            x = Number(x.toFixed(2));
            return x + "Bil"; 
        }
        if (x > 1000000) {
            x = x / 1000000
            x = Number(x.toFixed(2));
            return x + "Mil"; 
        }
        if (x < 1000000) {
            x = Number(x.toFixed(2));
            return x; 
        }

    }

Upvotes: 0

Vesper
Vesper

Reputation: 18747

You'd better write your function as proper function that can return a String value to assign to a text property or use elsewhere. Also, you should use a pattern that is easily extendable to bigger prefixes, should you need them. Say, I have found a game with a W prefix being used, which is one beyond the common "yotta" prefix, and there was a set of subsequent prefixes as well. So, this is how you should devise such a function:

function numtolet(x:Number):String {
    const prefixes:Vector.<String> = Vector.<String>(["","k","m","g","t"]); 
    // add more to taste. Empty prefix is used if the number is less than 1000
    var y:Number=x;
    var i:int=1;
    // provided x>0, if not, store a minus somewhere and attach later
    while((y>=1000) && (i<prefixes.length)) {
        y=y/1000;
        i++;
    } 
    // there, you have just divided X by 1000 a couple of times and selected the prefix
    var s:String = y.toFixed(2)+prefixes[i-1];
    // if there was a minus, add it here: s="-"+s;
    return s;
}

Then you just call it like this:

output.text=numtolet(earner);

Upvotes: 1

Scab
Scab

Reputation: 28

When you use the CHANGE event like Neguido said, and add listeners to different text fields, you can use e.target.text = to change the text in the calling text field. To target a different variable for each text field is more difficult, because you cant pass extra arguments into the event handlers, and you cant add your own variables/properties to textFields. You could stick each textField into a parent MovieClip and then create variables in there like MovieClip1.earner = 0 and retrieve the values with e.target.parent.earner. You could also write a dynamic extension to the TextField class where you add custom variables. Alternatively you could use a switch statement in your event handler to use different variables for different callers.

Upvotes: 0

user4152062
user4152062

Reputation:

You can do this using the CHANGE event:

output.addEventListener(Event.CHANGE, numtolet);

function numtolet(e:Event):void
{
    output.text = String(int(earner  * 100) / 100);
    if (earner >= 1000 && earner < 1000000)
    {
        output.text = String(int((earner/1000)  * 100) / 100 + "k");
    }
    else if (earner >=1000000 && earner < 1000000000)
    {
        output.text = String(int((earner/ 1000000) * 100 ) / 100 + " M");
    }
}

This will make the function run every time the text is changed by a user, but you'd probably want to add a few conditional (if)'s to the function, or use a variable to keep track of the curent number. When the number converts to 1k, how does it know what do to at 1000k?

Feel free to ask if you need help on this.

Upvotes: 0

Related Questions