Using a dynamic precision value in number_to_currency based on the decimal value?

The following will not work with normal floats, because of precision problems, but if you're using BigDecimal it should work fine.

Up vote 1 down vote favorite share g+ share fb share tw.

Thoughout our app we use number_to_currency(value, :precision => 2). However, we now have a requirement whereby the value may need displaying to three or more decimal places, e.g. 0.01 => "0.01" 10 => "10.00" 0.005 => "0.005" In our current implementation, the third example renders as: 0.005 => "0.01" What's the best approach for me to take here? Can number_to_currency be made to work for me?

If not, how do I determine how many decimal places a given floating point value should be displayed to? Sprintf("%g", value) comes close, but I can't figure out how to make it always honour a minimum of 2dp. Ruby-on-rails ruby decimal rounding link|improve this question asked Jul 23 '10 at 16:05Olly2,8321838 82% accept rate.

The following will not work with normal floats, because of precision problems, but if you're using BigDecimal it should work fine. Def variable_precision_currency(num, min_precision) prec = (num - num. Floor).

To_s. Length - 2 prec = min_precision if prec prec) end ruby-1.8.7-p248 > include ActionView::Helpers ruby-1.8.7-p248 > puts variable_precision_currency(BigDecimal. New("10"), 2) $10.00 ruby-1.8.7-p248 > puts variable_precision_currency(BigDecimal.

New("0"), 2) $0.00 ruby-1.8.7-p248 > puts variable_precision_currency(BigDecimal. New("12.45"), 2) $12.45 ruby-1.8.7-p248 > puts variable_precision_currency(BigDecimal. New("12.045"), 2) $12.045 ruby-1.8.7-p248 > puts variable_precision_currency(BigDecimal.

New("12.0075"), 2) $12.0075 ruby-1.8.7-p248 > puts variable_precision_currency(BigDecimal. New("-10"), 2) $-10.00 ruby-1.8.7-p248 > puts variable_precision_currency(BigDecimal. New("-12.00075"), 2) $-12.00075.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions