Your Universal Remote Control Center
RemoteCentral.com
Philips Pronto Professional Forum - View Post
Previous section Next section Previous page Next page Up level
Up level
The following page was printed from RemoteCentral.com:

Login:
Pass:
 
 

Page 1 of 2
Topic:
Changing an integer to an minimum length
This thread has 24 replies. Displaying posts 1 through 15.
Post 1 made on Saturday October 2, 2010 at 16:55
chudson
Lurking Member
Joined:
Posts:
October 2010
4
Hi guys, I need to change an integer to a minimum length of 3 places eg 1 would be 001. This is required for a peice of hardware I have built for dimming which works from 000 to 255. Any ideas on how to change the integer? I thought the java command setLength() would work but pronto won't have it. Thanks
Post 2 made on Saturday October 2, 2010 at 17:20
Barry Gordon
Founding Member
Joined:
Posts:
August 2001
2,157
try:
while ((a&"").length<3) a="0"&a;

the a+"" should convert a to type string since to do the concatenation it must be a string. Adding an empty string ("") to a string does not change the original string.

I have not tested the above.

Last edited by Barry Gordon on October 2, 2010 17:31.
OP | Post 3 made on Saturday October 2, 2010 at 17:32
chudson
Lurking Member
Joined:
Posts:
October 2010
4
Unfortunatly that doesnt seem to do the trick, no zeros have been appended.
Post 4 made on Sunday October 3, 2010 at 02:22
alpha
Long Time Member
Joined:
Posts:
September 2003
258
var dimming ;
If ( dimming = 0 ) { dimming = 000 ; };
Project Boredom 2 is here. [Link: mediafire.com]
------------------------
Check Version 1 & 2 out in the files section.
OP | Post 5 made on Sunday October 3, 2010 at 04:35
chudson
Lurking Member
Joined:
Posts:
October 2010
4
I already use that line but it doesnt solve the problem with numbers 1 to 99.
Post 6 made on Sunday October 3, 2010 at 05:18
Lyndel McGee
RC Moderator
Joined:
Posts:
August 2001
12,999
chudson,

In your first post, you mention java. It is not java, it is javascript. Java and javascript bare some similarities but they are completely different languages.

If you know the length you want, it is quicker to pre-build a string of 0's and use substring.

function zeroFill3 (value)
{
var ret = '000' + value;
var l = ret.length;
return ret.substring(l - 3,l);
}

If you don't know the length you need, use a loop to build out string. Barry's example may be quicker but the code below is what I used at one time for EscientPronto.

function zeroFillToPrecision(value,precision)
{
precision=0 - precision;
value=Math.abs(0-value);
var strValue = '' + value;
var valueLen = strValue.length;
if (valueLen >= precision) /* truncate length to precision.*/
return strValue.substring(valueLen-precision);
else
{
var ret = '';
for (var i=0;i {
ret += '0';
} // for
ret += strValue;
return ret.substring(ret.length-precision);
} // else
}
Lyndel McGee
Philips Pronto Addict/Beta Tester
Post 7 made on Sunday October 3, 2010 at 09:27
buzz
Super Member
Joined:
Posts:
May 2003
4,383
My pseudo code technique for this sort of issue has been:

last3characters( "00" || stringValueOf( number) )

"||" is my shorthand for string concatenation.

In interpreted languagues this is straight forward and relatively quick.

(This is simply a less general, single line version of Lyndel's scheme.)
Post 8 made on Sunday October 3, 2010 at 13:07
BluPhenix
Long Time Member
Joined:
Posts:
December 2008
371
Wow using an or to concatenate strings. Never seen this. I'm amazed it works, have o try it.

buzz but if I'm not mistaken, in your case:

"00" || stringValueOf(2) = "002"
"00" || stringValueOf(12) = "0012"

While with Lyndel's code the result of the first is "002" and the second is "012".

Also is the method stringValueOf necessary? I believe that in javascript : ""+12 = "12".

chudson, wait, you need to send those values as integers (so in hex 0x00 - 0xff) or in ascii? Plese make it more celar.
Post 9 made on Sunday October 3, 2010 at 14:43
Lyndel McGee
RC Moderator
Joined:
Posts:
August 2001
12,999
Thanks Buzz... Yes, I noticed the correct spelling. Much appreciated.

What is stringValueOf() function?

Last edited by Lyndel McGee on October 3, 2010 15:03.
Lyndel McGee
Philips Pronto Addict/Beta Tester
Post 10 made on Sunday October 3, 2010 at 14:57
buzz
Super Member
Joined:
Posts:
May 2003
4,383
BluPhenix,

Like Barry Gordon, I have too many computer languages circulating in my brain.

"||" in some languages is string concatenation, in others it could be an Exclusive OR or a simple OR.

In JavaScript string concatenation is "+". I don't like the "+" syntax because it is too easy to confuse with arithmetic.

This is why I tried to define my terms in the previous post. Because of where I've come from "||" is what I use for string concatenation in my brain. Mercifully, it usually comes out as "+" when I'm typing JavaScript.
Post 11 made on Sunday October 3, 2010 at 15:04
Lyndel McGee
RC Moderator
Joined:
Posts:
August 2001
12,999
What is stringValueOf() ????
Lyndel McGee
Philips Pronto Addict/Beta Tester
Post 12 made on Sunday October 3, 2010 at 15:36
BluPhenix
Long Time Member
Joined:
Posts:
December 2008
371
I guess is some function from another language :). Don't be to troubled by it :)

Anyhow, buzz's way in javascript is: "00"+number :).

But this exposes the issue i posted and that Lyndel's code solves ("0012") ...
Post 13 made on Sunday October 3, 2010 at 16:15
Lyndel McGee
RC Moderator
Joined:
Posts:
August 2001
12,999
Yes, "00" + myNumber will be "0012" if myNumber has integral value of 12. That is exactly why I posted my functions.

In the Escient protocol, you need zero-padded lengths of 2, 3, 4, and 5. I specifically have 4 different functions that do this versus the looping one as the string concat with specific length is much quicker than doing the looping to build a string of '0's with specific precision.
Lyndel McGee
Philips Pronto Addict/Beta Tester
Post 14 made on Sunday October 3, 2010 at 16:49
Barry Gordon
Founding Member
Joined:
Posts:
August 2001
2,157
Here is what I use (tested too)

function padIt (a,b) {c=a+""; while(c.length < b) {c="0"+c;} return c;}

for those not Prontoscript fluent:
c=a+""; causes c to be converted to a string in case 'a' was an integer and is necessary for the while test to work correctly as an integer has an undefined length. If a was already a string then concatenating it with an empty string does nothing

In Javascript the Plus(+) operator is overloaded and takes on two different operations; addition and concatenation. The overloading is resolved by the context in which the operator is used. This can run into unexpected issues when one wants to build a string that terminates in an integer number. For example

label="Item_";
System.print(label); produces "Item_"
label="Item_"+1;
System.print(label); produces "Item_1"
label="Item_"+1+2;
System.print(label); produces "Item_12"
label="Item_"+(1+2);
System.print(label); produces "Item_3"

The (1+2) causes that term to be evaluted arithmetically since the "(" starts a new context. In the case "Item_"+1+2 the evaluation proceeds from left to right with the left side providing the context. Ergo evaluating "Item_"+1 causes "1" to be converted to a string and then concatenated to "Item_" yielding "Item_1".

The two most bothersome attributes of Javascript (to me) are the fact that symbols are case sensitive and the overloading of "+". I was trained on languages where symbols are case insensitive. Most of my semantic errors in Javascript code I write are due to the symbols being case sensitive in Javascript; since most syntax checkers will not catch that

Last edited by Barry Gordon on October 5, 2010 20:36.
Post 15 made on Sunday October 3, 2010 at 17:13
BluPhenix
Long Time Member
Joined:
Posts:
December 2008
371
Different people, different tastes :)

I actually like the overloading of the + operator, I simply take it that it adds to something. In the case of 5+3 it adds 3 to 5 in the case of "number"+1 it adds 1 to number.

Maybe it's easyer to remember this way: if the most left value of a variable is a string then the whole variable is a string.

So Barry's c=a+"" would have the form of c=""+a in my code. To not forget this i even do:
myVariable=""+1+variable1+ariable2+"string"+"\r" so i know it is a string as soon I look over the = sign.

Barry if you don't yet, use JSLint (the good parts) it let's you define "members" (methods and properties) of objects and checks the code against the list you provided. It's case sensitive and if it finds a properts or method that is different from your list it alerts you. I can't work without it any more.
Page 1 of 2


Jump to


Protected Feature Before you can reply to a message...
You must first register for a Remote Central user account - it's fast and free! Or, if you already have an account, please login now.

Please read the following: Unsolicited commercial advertisements are absolutely not permitted on this forum. Other private buy & sell messages should be posted to our Marketplace. For information on how to advertise your service or product click here. Remote Central reserves the right to remove or modify any post that is deemed inappropriate.

Hosting Services by ipHouse