View Full Version : Use of commas in calculator text fields
njcarron
11-22-2002, 04:00 PM
I'm wondering if anyone knows if commas can be incorporated in input or dynamic text fields that are programmed to serve as numerical calculators.
For instance, I have 3 fields...
onClipEvent (load) {
varField1 = 0; - (input)
varField2 = 0; - (input)
varField3 = 0; - (dynamic)
}
//The fields formula is set up as...
varField3 = Number(varField1 * varField2);
If the user types in 1000 for both varField1 and varField2, varField3 will equal 1000000
What I'd ideally like is for the user to be able to type 1,000 for varField1 and varField2, and for varField3 to dynamically show 1,000,000. Obviously because a comma is being typed in, the fields read as NaN. This could cause confusion with the user and it makes reading large numbers easier.
Any way to make it work so that commas can be used?
Thanks!
Ricod
11-23-2002, 08:53 AM
I wouldn't know how to go about it, but what I think would work is to have the 3rd being build from the 1st and 2nd. (well duh ... RicoD ! obviously ...) but process it a bit first.
They will enter "1,000", for example. So, a string, not a number. Then you convert it into a number by counting the amount of characters used, knowing that 5 characters will be typed for a 1000-9999, 6 for 10000 - 99999 etc. Using the substring method you can then get the first (or first two / three) and the rest, without the commas.
I believe there's a piece of code in the 'library' *points up to the blue grayish bar* that converts strings into numbers or vice versa that uses pretty much the same method to change 10000 into "ten thousand".
LuxFX
11-25-2002, 04:02 PM
first, to do this you need to have separate variables for performing calculations and for displaying them -- essentially numeric variables and string variables.
converting from the display string to a number is not hard, to remove commas just do something like this:
var numValue = Number((strValue.split(',')).join(''));
then, while in numeric value you can perform the calculations. Then, to convert back to comma format, you can use a function like this (here are examples of how to use it also:
function toComma(num)
{
var cStr = num.toString();
var decPoint = cStr.indexOf('.');
if (decPoint == -1)
decPoint = cStr.length;
while ((decPoint = decPoint-3) > 0)
cStr = cStr.substring(0, decPoint) + "," + cStr.substr(decPoint);
return cStr;
}
trace("1000 -- " + toComma(1000));
trace("40 -- " + toComma(40));
trace("65.0032 -- " + toComma(65.0032));
trace("25320033.222 -- " + toComma(25320033.222));
Hope this is what you needed!
LuxFX
njcarron
12-03-2002, 06:14 PM
Thanks LuxFX,
Hopefully you are still around. I was out of the office for the last week with the Thanksgiving holiday, but have been trying to get this to work since...
Basically, I have my fields in a movie clip that I have programmed as follows...
onClipEvent(load){
field1 = 0;
field2 = 0;
field3 = 0;
}
onClipEvent(keyUp){
field3 = Number(field1) + Number(field2);
}
With the resolution that you passed on to me, how would I incorporate it into the code? I understand that since it's a function, it has to be incorporated in the onClipEvent. My main concern is to have the dynamic text field (field3) be able to understand the input fields if the user puts a comma in field1 and/or field2, and then be able to show the final total in field3 with a comma regardless of what was input.
Hopefully I clear. This project has been smooth sailing up to this point!
Thanks for any response!
LuxFX
12-04-2002, 02:12 PM
Hey, I just got back from Thanksgiving holidays myself!
Here's what you should do. In your main timeline on the first frame, define the function:
function toComma(num)
{
var cStr = num.toString();
var decPoint = cStr.indexOf('.');
if (decPoint == -1)
decPoint = cStr.length;
while ((decPoint = decPoint-3) > 0)
cStr = cStr.substring(0, decPoint) + "," + cStr.substr(decPoint);
return cStr;
}
then inside your onClipEvent(keyUp) you can use it and the other line of code I mentioned like this, using temporary variables to store the number-only versions of your field values:
onClipEvent(keyUp){
var n1 = Number((field1.split(',')).join(''));
var n2 = Number((field2.split(',')).join(''));
field3 = toComma(n1 + n2);
}
hope this helps!
njcarron
12-04-2002, 03:09 PM
I think I'm on my way - I need to hammer down how to use functions effectively, and this is one instance that they are definitely needed. Thanks for your time!
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.