PDA

View Full Version : Print text to 1 decimal place


Dave Taylor
03-06-2006, 09:15 AM
Hi very new to Flash & Action scripts.

Found the tutorial page the best yet.

var volts:Number;
volts = 12.5;
VoltMeter.text = volts + "V";
The above code prints the varible volts in a Dynamic Text Box
in another function I increase the volts, when it reaches 13 its dsiplayed as 13V, how can I get to show 13.0V, I have been playing with the formatter command but cant get it to function.

Regards, Dave

jsebrech
03-06-2006, 09:28 AM
Maybe this technical note (http://www.macromedia.com/cfusion/knowledgebase/index.cfm?id=tn_14267) on how to format numbers as currencies will help you out.

MichaelxxOA
03-06-2006, 09:29 AM
Here's a little formatter function that you pass the number into and the number of decimal places you'd like it to have, and it will return the adjust number as a string... here's how you'd use it in your code.


var volts:Number;
volts = 12.5;
VoltMeter.text = decimalPlaces(volts, 2) + "V";

function decimalPlaces (value, places)
{
var num_str = (value.toString ());
var dec_pos = num_str.indexOf (".");
var zeros = places - (num_str.length - dec_pos);
if (dec_pos == - 1)
{
num_str += ".";
}
for (var i = 0; i <= zeros; i ++)
{
num_str += "0";
}
return num_str;
}



EDIT:\\ DUMB error, sorry

Hope this helps, take care.


_Michael

Dave Taylor
03-06-2006, 03:41 PM
Many thanks I had to change

num_str += ".";
to
num_str += ".00";

otherwise 12.00V showed as 12.V

I assume thats correct

Thanks for a speedy reply

Regards, Dave