Page 1 of 1

money_format

PostPosted: Thu Feb 16, 2012 3:13 pm
by wisni1rr
I was wondering how to correctly use 'money_format' in my fields.ini file? I have read the wiki pages and cannot get it to function. I've been trying this:
[ListPrice]
money_format('%i', $ListPrice)


That should get me USD. I am actually looking for it to display in the format of $xxx,xxx.

Can anyone tell me what I am doing wrong?

Thanks!

Re: money_format

PostPosted: Fri Feb 17, 2012 5:34 pm
by shannah
I don't believe this directive is in 1.3.x... It is marked as requiring version 2.0 which isn't released yet.

The syntax would be something like:
Code: Select all
[ListPrice]
money_format="%i"



In 1.3.x you could achieve the same thing by implementing a ListPrice__display() method in your delegate class as follows:
Code: Select all
function ListPrice__display($record){
    return money_format('%i', $record->val('ListPrice'));
}


-Steve

Re: money_format

PostPosted: Mon Feb 20, 2012 12:28 pm
by wisni1rr
Thanks Steve,

I tried the following in my /tables/SALE/SALE.php:

Code: Select all
<?php
function ListPrice__display($record){
    return money_format('%i', $record->val('ListPrice'));
}
?>


and php is generating this error:

Code: Select all
Fatal error: Class 'tables_SALE' not found in [rootPATHomitted]xataface/Dataface/Table.php on line 1116


Not sure what's happening.

Re: money_format

PostPosted: Mon Feb 20, 2012 1:12 pm
by shannah
That function has to be defined inside the table delegate class.
Please read the following section of the getting started guide for more info on how to create a table delegate class:
http://xataface.com/documentation/tutor ... te_classes

Re: money_format

PostPosted: Mon Feb 20, 2012 7:36 pm
by wisni1rr
Thank you.

I was missing the class tables statement:

class tables_SALE {


However, the format function is not behaving as I'd like it to (I'm sure it is acting correctly as it is programmed, I need to modify the function.

It now displays the price as "5000.00". I am looking for it to display as "$5000".

Thanks for your help

Re: money_format

PostPosted: Mon Feb 20, 2012 9:15 pm
by shannah
Check out the php documentation page for money_format. There are some examples there.
http://www.php.net/money_format

The money_format will depend on the locale that is set. Sometimes I just use the number_format() function instead, and prepend a '$' to it.

-Steve

Re: money_format

PostPosted: Tue Feb 21, 2012 4:10 pm
by ADobkin
I use the following:

For version 1.5.x or 2.x, in fields.ini:
Code: Select all
[ListPrice]
locale = "en_US"
money_format = "%n"


For version 1.3.x, in the table delegate class:
Code: Select all
function ListPrice__display($record){
    setlocale(LC_MONETARY, 'en_US');
    return money_format('%n', $record->val('ListPrice'));
}


The %n (instead of %i) in this case prepends the '$', although Steve's solution to add it manually and use number_format instead may be easier if you only need to consider USD.

Re: money_format

PostPosted: Thu Feb 23, 2012 9:42 am
by wisni1rr
Thanks Adobkin!

The function you create works fine.

Is it possible to have it omit the decimals. All my values are whole numbers for the field in question.

Thanks again

Re: money_format

PostPosted: Thu Feb 23, 2012 10:51 am
by ADobkin
Yes, you need the right precision parameter. Try the string "%.0n" as your money_format value. For more details, see:

http://php.net/money_format

Alan

Re: money_format

PostPosted: Thu Feb 23, 2012 12:11 pm
by wisni1rr
Fantastic.

Thank you, Alan!!

-Rich

Re: money_format

PostPosted: Tue Mar 13, 2012 1:11 pm
by wisni1rr
I am trying to format the output on my custom function. The following is from a table delegate function section.

Code: Select all
$childOrigListPrice = money_format('%.0n', $child->val('OrigListPrice'));


However, when I call $childOrigListPrice in my content array, the number is not formatted.

Any Ideas?

Re: money_format

PostPosted: Tue Mar 13, 2012 1:20 pm
by wisni1rr
SOLVED

Missing the following

Code: Select all
setlocale(LC_MONETARY, 'en_US')