money_format

A place for users and developers of the Xataface to discuss and receive support.

money_format

Postby wisni1rr » Thu Feb 16, 2012 3:13 pm

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!
wisni1rr
 
Posts: 107
Joined: Mon Feb 13, 2012 9:03 pm

Re: money_format

Postby shannah » Fri Feb 17, 2012 5:34 pm

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
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: money_format

Postby wisni1rr » Mon Feb 20, 2012 12:28 pm

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.
wisni1rr
 
Posts: 107
Joined: Mon Feb 13, 2012 9:03 pm

Re: money_format

Postby shannah » Mon Feb 20, 2012 1:12 pm

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
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: money_format

Postby wisni1rr » Mon Feb 20, 2012 7:36 pm

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
wisni1rr
 
Posts: 107
Joined: Mon Feb 13, 2012 9:03 pm

Re: money_format

Postby shannah » Mon Feb 20, 2012 9:15 pm

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
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: money_format

Postby ADobkin » Tue Feb 21, 2012 4:10 pm

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.
ADobkin
 
Posts: 195
Joined: Mon Oct 22, 2007 7:31 pm
Location: Atlanta, GA, USA

Re: money_format

Postby wisni1rr » Thu Feb 23, 2012 9:42 am

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
wisni1rr
 
Posts: 107
Joined: Mon Feb 13, 2012 9:03 pm

Re: money_format

Postby ADobkin » Thu Feb 23, 2012 10:51 am

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
ADobkin
 
Posts: 195
Joined: Mon Oct 22, 2007 7:31 pm
Location: Atlanta, GA, USA

Re: money_format

Postby wisni1rr » Thu Feb 23, 2012 12:11 pm

Fantastic.

Thank you, Alan!!

-Rich
wisni1rr
 
Posts: 107
Joined: Mon Feb 13, 2012 9:03 pm

Re: money_format

Postby wisni1rr » Tue Mar 13, 2012 1:11 pm

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?
wisni1rr
 
Posts: 107
Joined: Mon Feb 13, 2012 9:03 pm

Re: money_format

Postby wisni1rr » Tue Mar 13, 2012 1:20 pm

SOLVED

Missing the following

Code: Select all
setlocale(LC_MONETARY, 'en_US')
wisni1rr
 
Posts: 107
Joined: Mon Feb 13, 2012 9:03 pm


Return to Xataface Users

Who is online

Users browsing this forum: No registered users and 20 guests

cron
Powered by Dataface
© 2005-2007 Steve Hannah All rights reserved