4

I have written a function to_money so that 'Price' 'Quantity' and 'Total' in the sub_total function are formatted and two zeroes attached - so 2 becomes 2.00, the function is here:

 to_money: function(amount) {
      return Number(amount).toFixed(2) 
    },


sub_total: function() {
 var In = this
 return $$('.item').inject(0, function(sum, row) {
  var quantity = Number($F('Item' + row.id + 'Quantity'))
  var price = Number($F('Item' + row.id + 'Price'))
  var line_total = quantity * price
 $('Item' + row.id + 'Quantity').value = In.to_money(quantity) 
  $('Item' + row.id + 'Price').value = In.to_money(price)
  $('Item' + row.id + 'Total').update('£' + In.to_money(line_total)) In.to_money(line_total)) 
  return sum + line_total 
 })

How do I write a function that is similar to the function 'to money' that formats the price, but instead a function that formats the quantity for making sure a default decimal of 1 is prefixed to the quantity if no input is entered.

so the line in function sub_total would call the the new function to run on quantity:

 $('Item' + row.id + 'Quantity').value = In.to_decimal(quantity) 

Would the function look like this?

to_decimal: function(amount) {
          return Number(amount).toFixed(0) 
        },
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Ben Aidley
  • 347
  • 4
  • 10
  • Not sure what you mean. My interpretation would be that you want a zero/no value to display as `0.1`? – KooiInc May 05 '12 at 08:06
  • I'm trying make it so that that if nothing is entered into quantity the js automatically adds in the decimal number 1 in. – Ben Aidley May 05 '12 at 08:08
  • You still haven't said what "the decimal number 1" means. It is 1.0? Or 0.1? Or something else? "1" on its own isn't necessarily a decimal number: it's 1 in any base (including binary). – Andrew Leach May 05 '12 at 08:15

1 Answers1

3

try

to_decimal: function(amount) {
          var n = Number(amount);
          return (n && n>0 ? n : 1).toFixed(2);
}
In.to_decimal('');               //=> 1.00
In.to_decimal('bogusinput');     //=> 1.00
In.to_decimal(0);                //=> 1.00
In.to_decimal('23.1');           //=> 23.10
//note: Number autotrims the parameter
In.to_decimal('          45.3'); //=> 45.30
KooiInc
  • 119,216
  • 31
  • 141
  • 177
  • `1..toFixed(2)` is equivalent to `'1.00'` (and perhaps more readable). I'd use `to_decimal: function(n) {return (n>0 ? 1*n : 1).toFixed(2);}` – Rob W May 05 '12 at 08:15
  • 1
    @Rob W: not really, `return (n>0 ? 1*n : 1).toFixed(2);` would sometimes return `NaN`, e.g. in `In.to_decimal('bogusinput');` – KooiInc May 05 '12 at 08:22
  • I see. The following is waterproof though: `(n > 0 && 1*n || 1).toFixed(2)` (save for infinity values, ([`"1e999"`](http://stackoverflow.com/a/8112802/938089?are-there-are-any-side-effects-of-using-this-method-to-convert-a-string-to-an-in) and `Infinity`)). – Rob W May 05 '12 at 08:25
  • Yep. Looks like micro optimization and a matter of choice/taste. In both cases '1e999' would return 'Infinity'. If you wanted to prevent that you would check `n – KooiInc May 05 '12 at 08:32
  • And if you really want to be sure you always get a stringified number in the form `[nnn...].[nn]`, you would check `n<=1e20` – KooiInc May 05 '12 at 08:39