I wanted to use the "decimal" type to store a price, but it was not available.

I see there is "float", but it's not exactly the same, and in some cases it's better to use "decimal".

So I wrote this little patch. It works for me using computed_field 6.x-1.0-beta3

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tomsm’s picture

Nice feature. I hope it will be implemented soon.

cYu’s picture

I will be testing this out soon as well. Using float makes a computed money amount like $10,000.45 round to $10,000.50 regardless of what length I use in config of the field.

fabio84’s picture

I found out that 'length' is ignored in the "schema definition" for numeric (decimal) and float fields.

According http://drupal.org/node/146939 and http://drupal.org/node/159605 , you have to use 'precision' and 'scale' for numeric, and 'size' => 'big' to have a double instead of float.

So with my new patch you can specify these parameters correctly: you can still enter "PRECISION,SCALE" in the length field, and it will be splitted in the two parameters; and for float if you enter a value bigger than 7 (the maximum precision of float) it will use a double.

It seems to work for me

fehin’s picture

Hi fabio84, your patch is a little different from what I'm used to. What does the ! represent? Should I only be concerned with the lines that start with +?

I'm trying to get my field to display something like 500.00 or 500.90. Right now it drops the 0 after the decimal point so I see 500 and 500.9 instead. Thanks.

**Update**
I got the patch to work. It did exactly what I needed. Thanks for the patch.

zeezhao’s picture

Thanks for the patch. It works fine for me.

In views though, please what is the best way to get the decimal computed field displayed with commas i.e.
instead of 1000000.00 to have 1,000,000.00

I tried using number_format(), sprintf() functions in the computed field calculation, but when it gets saved as decimal the number is truncated in views. Thanks

edit:
Now got it to work.
1. Used this in display format: $display = number_format($node_field_item['value'], 2);
2. And then make user both display for field & views uses "Plain Text"

not_Dries_Buytaert’s picture

I support this feature request, as in many of my use-cases I need to automatically calculate currencies.

I can't test patches, as I can't apply them (on Windows). The instructions are just way too technical and tedious for non-techincal functional testers, like me: http://drupal.org/node/60179. :-( Searched the internet to learn whether Notepad++ (with a plug-in perhaps) could patch easily. Also tried (without success) installing and using Eclipse, TortoiseSVN and WinMerge. So please, feel free to build an online tool or Drupal module for patching. ;-)

Anyway, the patch might still need input and display options to set the:
1) number of postfixing zero's/ digits (padding) and
2) symbol (comma or dot) for in between the thousands and for prefixing the digits.

If these options have been implemented in the patch already and given the test results from post #4 & #5, I suggest someone changes the status of this issue to "reviewed & tested by the community"

fabio84’s picture

Status: Needs review » Needs work
FileSize
3.22 KB

Sorry, the patch was not in the "unified" format.

Now this is in the right format, but... It's still the patch for 6.x-1.0-beta3, so I hope the maintainer or someone else is willing to merge it to the dev version.

fehin’s picture

Will this ever be added to the module? Can I use it to patch the current version 6.x-1.0-beta5?

Moonshine’s picture

Status: Needs work » Fixed

Interesting, I actually added this to the dev release a couple days ago without even seeing this thread. :) People can give the dev tarballs a try. (It also has some other fixes and data configurations)

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.