Digit detection before and after decimal place

Mar 7, 2010 at 11:55pm

Digit detection before and after decimal place

Hello,

I am trying to determine the number of digits in a float number before and after the decimal place.

I have been able to find this code, however as yet it only determines post decimal place digit count:

// use dec_sep for internationalization
function Decimals(x, dec_sep)
{
var tmp=new String();
tmp=x;
if (tmp.indexOf(dec_sep)>-1)
return tmp.length-tmp.indexOf(dec_sep)-1;
else
return 0;

I have managed to create this out of it, however it creates some error messages:

// Javascript to determine number of decimal digits

inlets = 1;
outlets = 1;

// use dec_sep for internationalization
function msg_float(x, r)
{
var tmp=new String();
tmp=x;
if (tmp.indexOf(r)>-1)
return tmp.length-tmp.indexOf(r)-1;
else
return 0;}

js: Untitled.js: Javascript TypeError: tmp.indexOf is not a function, line 10
js: error calling function msg_float

I know that a simple way of calculating pre decimal length would be to simply subtraction the number of decimal place digits from the total number of digits.

Can anyone help?

Is using javascript the best method?

Cheers
Dan

#48993
Mar 8, 2010 at 1:02am

actually, counting the number of digits after the decimal point doesn’t make much sense… the computer doesn’t store the number that way. You can tell javascript to make a string out of your number with whatever level of precision you would like (up to some limit, of course).

As for counting the number of digits before the decimal point… there are a few things wrong with your function.

First, your error. x is a number (it has to be, because max will only decide to call this function if it gets a number as the input). indexOf is a method on strings. You need to convert x into a string before you can do this… x.toString() should be fine for this purpose.

Assigning the value of x to a variable tmp the way you are is useless. The sample code is basically doing:

var tmp = new String();
tmp = x;

to get javascript’s automatic conversion from number to string. If you find it necessary to store the string value, simply use var tmp = x.toString();

/brian

#176026
Mar 8, 2010 at 1:06am

There are a few things you’re trying to do which don’t work the way you expect. Firstly when you use msg_float() to grab the number you send to your [js] object it will only have one argument: the input number. You can’t specify the seperator as well. For that you’d have to use function anything() and send it a list.

Inside this function you are declaring a new variable called temp and specifying it as a string, then you are setting it to your received value which is a number. Javascript is dynamic so this will override your previous statement and temp will be a number. This is what is causing your error as numbers don’t have an indexOf() method.

Seeing as floats cannot show all possible decimal numbers you will find that you get the value after the decimal place represented by six digits quite a bit, more so for larger numbers. Your best bet is to send in a symbol instead of a float. Use a message box with “quotes” around the number, or [textedit] if you need user input.

Javascript represents numbers with more precision than max displays so if you have to send a number you can then use something like the code below to do the calculation. First chop it to six decimal places, convert it to a string and then remove any trailing zeros. Now you can use the indexOf() method to count the digits on either side of the decimal place.

lh


function msg_float(x) {
    x = x.toFixed(6).toString().replace(/0+$/,"");
    etc...
}
#176027
Mar 8, 2010 at 5:28am

luke, i too was going to write the same thing about msg_float and the number of args, but then i thought about it. it seemed to me that given the way javascript works, it probably would work to pass multiple arguments… so i tried it and it does work. i realize it isn’t documented behavior, so relying on it is not necessarily a good idea.

#176028

You must be logged in to reply to this topic.