By mkoskenk on
Hi!
I'm using a computed field to calculate data from two other fields and as the result is total sum in euros, I'd like to force the result to be formatted with two decimals. I've tried php function money_format() but it doesn't seem to do anything. I'm sure this is something really simple cos I remember doing that somewhere in the past but now for some reason I can't get it to work.
Here's the code snippet from cck field:
$sum = $node->field_t1_kpl[0]['value'] * $node->field_t1_hinta[0]['value'];
$decimals = $node_field[0]['value'] = number_format($sum, 2);
Can anyone point where I'm going astray?
Cheers,
Mikko
Comments
Edit
Really ought to read the post through before sending it...
The function I used is number_format(), not money_format() and the code snippet is as follows:
$sum = $node->field_t1_kpl[0]['value'] * $node->field_t1_hinta[0]['value'];
$node_field[0]['value'] = number_format($sum, 2);
update
Anyone have anything on this? The prob's still valid....
same problem
I'm using number_format function as well, trying to force two decimal places to the right and comma for thousands separator. Doesn't seem to work. Did you figure this out?
Workaround
Hi leahecooper,
coming back to the problem after some while I figured out a kind of workaround. It seems that it's impossible to store the formatted numbers in drupal's tables, but the trick was to apply the number formatting upon calling the value. I already had created custom templates for my content types and just added the formatting there when retrieving the value from database:
Hope this helps you out, and feel free to ask more if I was unclear explaining the solution!
And as an afterthought which
And as an afterthought which just occured to me, I don't think I tried saving the formatted value into the 'view' value of the variable. So in this case, I was just saving the value into the variable $node->field_total[0]['value'] instead of trying to store it into $node->field_total[0]['view']. It _might_ work out, but I don't have time to test it out right now. If someone has more info on this, please reply back. I'll try to find some time at some point to try this on out as well.
here's what i did
i tried a slightly different tact, but found this post very useful.
here's what i found important:
1) perform the calculations in the CCK computed field as you normally would. i have all values stored as float with the data length: 10,2
2) on the display i have this:
$display = number_format($node_field_item['value'],2, '.', ' ');
3) on the view, when you display the computed field, make sure that the format for the field in the view is 'Raw Text' instead of 'Computed Value' or another format.
Presto, no endless decimals on the view. :)
let me know what 'y'all find.
--
anawillem
http://jellobrain.com
Formatting Computed Result field as currency
Hi
I had the same challenge and fixed it like this:
In Content Type > Display Fields, I selected to display 'Raw Data'
The in formatting the $display variable, I did:
$display = '€' . number_format($node_field_item['value']);
That displays as €1,234
If I wanted two places of decimals I'd have gone with:
$display = '€' . number_format($node_field_item['value'],2);
That displays as €1,234.56
Hope this helps somebody else
Mike
Thaks for your share
@MikeMcCormac & @anawillem
Thanks, your post is very usefull. It's help my work
Thanks @MikeMcCormac. I was
Thanks @MikeMcCormac. I was looking at the wrong place (in the Views fields settings). Just to clarify, the correct place was the Computed Content Type settings.
Putting the Views display
Putting the Views display format on "Raw text" was the key to solving my problem. Thanks for that tip.