Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
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
Comment | File | Size | Author |
---|---|---|---|
#7 | computed_field-1.0-beta3-decimal-3.patch | 3.22 KB | fabio84 |
#3 | computed_field-decimal-2.patch | 4.33 KB | fabio84 |
computed_field-decimal.patch | 1.73 KB | fabio84 |
Comments
Comment #1
tomsm CreditAttribution: tomsm commentedNice feature. I hope it will be implemented soon.
Comment #2
cYu CreditAttribution: cYu commentedI 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.
Comment #3
fabio84I 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
Comment #4
fehin CreditAttribution: fehin commentedHi 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.
Comment #5
zeezhao CreditAttribution: zeezhao commentedThanks 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"
Comment #6
not_Dries_Buytaert CreditAttribution: not_Dries_Buytaert commentedI 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"
Comment #7
fabio84Sorry, 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.
Comment #8
fehin CreditAttribution: fehin commentedWill this ever be added to the module? Can I use it to patch the current version 6.x-1.0-beta5?
Comment #9
Moonshine CreditAttribution: Moonshine commentedInteresting, 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)