So I have a number in a view field that has to be "unformatted" to allow a math expression to process it. Otherwise it gets interpreted as a string and evaluated to zero. So the math works, but the results of the math now shows as a straight number without decimals. In other words, a value of 3500.00 doing a math expression of x2 gives a result of 7000 (no decimals). When I use a field template to format it back to looking like currency the last two digits of the whole number get interpreted as cents, giving a result of $70.00.

The template code is print money_format('$%i', $output);

Any suggestions as how to capture the whole number and not a number effectively devided by 100?

Using D7 and Views 3

Comments

John_B’s picture

It is best to take a step back and clarify whether your number field is storing an integer or a float in the database. For e-commerce uses, there is a tendency to use integer for prices and divide by 100 (i.e. storing the dollar etc. price in cents), because with floats there is a risk of rounding errors which can annoy customers who may feel overcharged. However, with relatively small figures like $70.00 rounding errors will be rare, and if you are not taking money it is probably safe to use floats. If you are using integers then AFAIK you will need to do some custom work to format with two-place decimals. See http://drupal.stackexchange.com/questions/31741/how-to-add-number-format...

I know that is only half an answer... still, it is the first point to sort out.

Digit Professionals specialising in Drupal, WordPress & CiviCRM support for publishers in non-profit and related sectors

hondaman900’s picture

Thanks John. The original values, entered by the user in an entityForm, are decimal. The fiels adds "$" prefix, comma thou separator and decimal point, but these mess up math expressions which I use in my View. So I select "unformatted" in the Views field so that the math works. That removes the formatting other than the decimal point. Then the math expression (where I'm subtracting one decimal from another decimal) all works correctly, but the math expression removes the trailing cents (whether zero or not), but otherwise gives a correct number. I don't see any option in the global math expression to adjust precision or adjust field format to avoid the trimming. There's an option to round or not but either setting doesn't change this.

Losing the cents is not a concern as I don't need them, it's that applying any theme/PHP formatting to display this number now always seems to treat the last two digits as cents and sticks a decimal there. If I use a PHP format with no decimal places it simply doesn't show the last two numbers, so I still lose them. Either way it's the same as dividing by 100.

Here's an illustration:

Decimal A: $12,000.25
Decimal B: $500.00
Views Decimal A Unformatted: 12000.25
Views Decimal B Unformatted: 500.00
Views Global math expression Decimal A - Decimal B = 11500
Applying views field template (print money_format('$%i', $output);) gives $115.00

Get the same results with: printf("$%01f", $output);

Any thoughts? It really looks like I'm missing something in the PHP code to format the field as the math in Views correct and is not behaving as if the number is being divided by zero. Strange...

John_B’s picture

Looks as though the issue and patch here https://www.drupal.org/node/2451775 are relevant. You could try the patch, and ideally review it at that issue.

Digit Professionals specialising in Drupal, WordPress & CiviCRM support for publishers in non-profit and related sectors