Page 1 of 1

presenting a number as a code

PostPosted: Mon Mar 21, 2005 11:22 pm
by Thunder
My solution calls for prices (a number) to be presented in code format. The idea is simple, X = 0, P = 1, C = 2, L = 3, G = 4 etc.

I need to be able to present prices as their equivalent code. eg, 123.00 would be PCL.00. The Filemaker solution this is replacing used 79 if statements to assign the correct code to anything up to 99,999.00. That solution has got to be the least elegant method of doing this (I was proud of it when I first made it but Servoy's magnificence requires elegance and simplicity when coding and that just wont do).

My trouble is that I am not sure how to start with getting this going. I have a feeling I need to convert number to a string and have gone so far as to assign each number to a variable as above, I'm in my Servoy Programming for Filemaker developers manual which goes on about NumToText (and its Servoy equivalent of .tostring(radix). I am just unclear on how to implement it and examples seem to be thin on the ground.

Thanks for any help you can give.

Bevil Templeton-Smith

PostPosted: Mon Mar 21, 2005 11:47 pm
by airmark
Bevil,

If I understand what you're trying to do, the following should work.

Code: Select all
var priceCode = number_field.toString()
priceCode = utils.stringReplace(priceCode, '1', 'P')
priceCode = utils.stringReplace(priceCode, '2', 'C')
priceCode = utils.stringReplace(priceCode, '3', 'L')
priceCode = utils.stringReplace(priceCode, '4', 'G')
price_code_field = priceCode

PostPosted: Mon Mar 21, 2005 11:58 pm
by Thunder
Greg, that is EXACTLY what I meant and was after. Thank you very much, works perfectly and is 1000 x more elegant than my dodgy if statement.

PostPosted: Tue Mar 22, 2005 12:06 am
by Thunder
and may I also add...

Servoy ROCKS!!!