- 
                Notifications
    
You must be signed in to change notification settings  - Fork 147
 
Open
Labels
Description
Hi,
I noticed a weird problem when using a fromCents instance when performing further calculations down the line, please see the example below:
  const itemSubTotalFromCents = currency('800000', { fromCents: true, precision: 4 })
  const itemSubTotalWithoutFromCents = currency('80.00', { precision: 4 })
  
  const invoiceDiscount = currency('20.00', { precision: 4 })
  const invoiceSubTotal = currency('80.00', { precision: 4 })
  const resultWithFromCents = invoiceDiscount.multiply(itemSubTotalFromCents.divide(invoiceSubTotal))
  
  // returns { intValue: 20, value: 0.002, s: Object, p: 10000 }
  
  const resultWithoutFromCents = invoiceDiscount.multiply(itemSubTotalWithoutFromCents.divide(invoiceSubTotal))
  
  // returns { intValue: 200000, value: 20, s: Object, p: 10000 }You can see that the first example which uses the fromCents looks incorrect, if you don't pass this option I get the expected result. If possible can you confirm whether this is a bug or not? I've only had a quick look at this but any insight would be greatly appreciated. I wouldn't expect the fromCents option to be carried forward in the returning instance.
In order to get round this problem I can just wrap with another instance of currency and this works:
const itemSubTotalFromCentsWithWorkAround = currency(currency('800000', { fromCents: true, precision: 4 }), { precision: 4 })
const resultWithWorkaround = invoiceDiscount.multiply(itemSubTotalFromCentsWithWorkAround.divide(invoiceSubTotal))
// returns { intValue: 200000, value: 20, s: Object, p: 10000 }Note I use fromCents originally as the BE returns the value in centi cents
ryant26