Flash Actionscript Number Format Function

Most people who read this are not Flash developers, but I know some of you are, so I thought I’d post this little script I made.

I searched all over the web for a simple function that would format numbers nicely in Flash. I couldn’t find one I liked, so I put this one together. Feel free to copy and paste it as you will, if you think you might find it useful.

It takes 4 parameters – the number you’d like to format, the number of digits you’d like for the decimal, the strign you’d like to use as a seperator for thousands, and the string to use for the decimal character.

function numberFormat(myNum:Number, precision:Number, seperator:String, decimal:String) {
	// get a string
	myString = String(myNum);
	// if there is a decimal…
	if (myString.indexOf(".", 0) != -1) {
		decimalIndex = myString.indexOf(".", 0);
		decimalNum = myString.substring(decimalIndex, myString.length);
		// otherwise
	} else {
		decimalIndex = myString.length;
		decimalNum = ".";
	}
	// split the number at the decimal
	number = myString.substring(0, decimalIndex);
	// make a new var that we will create with the commas
	newNumber = "";
	// add the commas…
	for (i=number.length-1; i>=0; i--) {
		newNumber = newNumber+number.charAt(number.length-1-i);
		if ((i%3 == 0) and (i != 0)) {
			newNumber = newNumber+seperator;
		}
	}
	// now make the decimal side - first, trim off the "."
	decimalNum = decimalNum.substring(1, decimalNum.length);
	// if it's the appropriate number of digits already, we are done!
	// otherwise…
	if (!(decimalNum.length == precision)) {
		// if we just need to add zeros,
		if ((decimalNum == 0) or (decimalNum.length0) {
		return newNumber+decimal+decimalNum;
	} else {
		return newNumber;
	}
}
trace(numberFormat(123456789, 0, ",", "."));
// returns "123,456,789"
trace(numberFormat(12345.6789, 1, " ", ","));
// returns "12 345,7"
trace(numberFormat(12345678.9, 2, ",", "."));
// returns "12,345,678.90"

Ask and ye shall reduce!

A while back, I wrote a plea to relieve me of my busted monitor. Thanks be to Matt, who emailed me and arranged to pick it up. I asked Matt how he heard of me – apparently he found my name while researching a courses that I used to teach at Camosun College, and found my blog and business site from there. Next thing you know, he’s the proud new owner of a free busted monitor.

He said he had a buddy with a monitor that had a similar problem and had a way to fix it. For a moment I cringed and thought, “Crap! I could have fixed it!” but then I remembered the months of being annoyed with it, trying various algorithms of unplugging it from the wall and powering it on and off, and realized I am much happier with my new monitor that just works.

Best of luck, Matt!

Summertime in Victoria

So far, summertime in Victoria is pretty good. Work-wise, I have been chanting a mantra lately of “bring it off”, and it’s because I want weekends, and to leave the office at 5:00, even if it isn’t every day, maybe just once in a while!

And it has been working. In the last 4 weekends we’ve been camping for 3 of them. This coming weekend, we might just make that 4 for 5. The van makes it easy, and we are getting our camping systems down, but the real kick for us ex-Vancouverites is that in Friday rush hour traffic, it takes about 40 minutes to get from downtown to the nearest provincial campground. when there isn’t any traffic, it’s more like 25 minutes. Granted, the nightlife scene in the middle of Victoria is much akin to the nightlife scene in the middle of the woods, but there’s a flipside to that token.

I have started to go sea kayaking after work with some guys at the office. Get this – from my desk, I get up, take the elevator downstairs, walk down the block and around the corner, and that’s where the kayak place is.  A mere $20 bill gets you a boat and the necessary gear from 5:00 til 8:00pm. I went last week, and again this week, and I think I am going to try for once a week as long as the weather is pleasant enough to keep it enjoyable.

Rathtrevor Beach

image

Last November, I traded my old motorcycle for a campervan. Then, in April, my brother came out and we did some work to make it more roadworthy. Finally, 6 months after acquiring it, Julie and I went camping in it. Whew! I was hoping that whole effort was going somewhere.

We drove up to Rathtrevor Beach, north of Nanaimo. The sunsets were something else! We spent 2 rainy days enjoying intermittent walks on the beach when it there was a moment of dry weather, and lots of quality time appreciating the van instead of tent option, particularly when it was wet outside.

I have come a long way in my camping career. It used to be I walked out into the woods with a bunch of boy scout buddies. We’d walk until we felt we couldn’t anymore, then we’d set up a tent, or sometimes a lean-to, and cook our supper squatted over coleman peak1 camp stoves. Hah! I am much more advanced now. It’s like I am hardly camping at all! I was pondering this while I was sitting on the couch in the van, talking with my parents on my cel phone, enjoying the warmth through that came from the furnace, a cool root beer poured from the fridge, with a belly full of chili I made on the 3 burner stove, having just done the dishes in the kitchen sink which I brought with me.

Okay, this experience may have lost something from the hiking days, but there really is something to be said for enjoying the great outdoors by looking out the van window, waking up from a good nights sleep under snuggy covers on a double bed with REAL PILLOWS.

We have 2 weeks planned in the van later this summer. We are going to keep ourselves on the island, partially because now that we are here we want to enjoy it, and partially because while the van is great in many ways, on gas is not one of them. I can hardly wait!