Script to format a currency value

Peter Haworth pete at mollysrevenge.com
Tue Mar 1 19:33:28 CST 2011


Hi Alex,
To be honest, I'm not sure if there's a real life case for variable sizing of groups!  I'm using the specs for the information returned by the Unix locale LC_MONETARY command and it specifically talks about the group spec this way.  Seems unlikely but  it adds to the spice of writing a script to deal with it!

I have come across some currencies where the neg sign comes after the currency symbol so I think I will have to add another parameter for that but For now, always make the neg sign either the first char or the last char in the string, so "-$12.34" and "12.34$-".

Good question on the zero issue.  The first thing I realised with this is that if the number of decimals is greater than the number of digits passed in,  zero-filling is needed.  For example, if I pass in 5 and the number of decimals is 2, then the numbers after the decimal need to be 05, not 5.  But in answer to your question, there should be a zero before the decimal point if the number is less than 1, so $0.05 or -$0.05.

Thanks!

Pete Haworth

-------------- next part --------------






http://www.mollysrevenge.com
http://www.sonicbids.com/MollysRevenge
http://www.myspace.com/mollysrevengeband








On Mar 1, 2011, at 4:42 PM, Alex Tweedly wrote:

> I'm curious  is there really a real-life use-case for the group spec ? i.e. variable size groupings of digits ?
> 
> Not sure what order you want for currency and neg sign ?   Should it be
>   $-123  or -$123
> and should it be 123-$  or  123$-  ?
> 
> Should there always be a 0 before the "point" ?  i.e. could the result   be   .12  rather than 0.12 ?
> How about -.12 rather than -0.12 ?
> 
> Thanks for a fun challenge ....
> -- Alex.
> 
> 
> 
> On 01/03/2011 04:55, Peter Haworth wrote:
>> OK, here's an amusing exercise in scripting for you experts out there, but I'm giving away a prize!  I will give the person who provides the best solution a copy of my band's latest CD, Aged 10 years.  the definition of "best" is strictly my opinion!  If you don;t like traditional Celtic music (including bagpipes), we;ll just do the script for the fun of it and sell the CD on eBay!!!
>> 
>> You have an integer value that needs to be formatted into a currency value.  It is stored as a positive or negative binary value no matter how many decimal places it has (eg 10000 = $100.00). You have the following formatting characters to deal with:
>> 
>> currency symbol: always present
>> 
>> currency symbol position: 1 = before the number, 0=after the number
>> 
>> decimal delimiter :  could be empty
>> 
>> number of decimals: could be zero and will be if decimal point char is empty
>> 
>> negative sign character: always present
>> 
>> negative sign position: 1= at the start of the formatted number , 0= at the end of the formatted number
>> 
>> group separator character: Specifies the character separator used for grouping digits to the left of the decimal delimiter in formatted monetary quantities.  Could be present even if no decimal delimiter is specified.  Also could be empty, meaning no grouping.
>> 
>> grouping spec:  Specifies a string that defines the size of each group of digits in formatted monetary quantities. Only present if the group separator is not empty. Consists of a sequence of semicolon-separated integers. Each integer specifies the number of digits in a group. The initial integer defines the size of the group immediately to the left of the decimal delimiter. The following integers define succeeding groups to the left of the previous group. If the last integer is not -1, the size of the previous group (if any) is repeatedly used for the remainder of the digits. If the last integer is -1, no further grouping is performed.
>> 
>> Deadline is midnight Friday March 4th, California time.  Good luck!
>> 
>> Pete Haworth
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> _______________________________________________
>> use-livecode mailing list
>> use-livecode at lists.runrev.com
>> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
>> http://lists.runrev.com/mailman/listinfo/use-livecode
>> 
> 
> 
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
> 



More information about the use-livecode mailing list