Currency: Difference between revisions

597 bytes removed ,  3 years ago
→‎{{header|Wren}}: Rewritten to use new BigRat class.
m (→‎{{header|REXX}}: added/changed whitespace and comments.)
(→‎{{header|Wren}}: Rewritten to use new BigRat class.)
Line 1,586:
=={{header|Wren}}==
{{libheader|Wren-big}}
<lang ecmascript>import "/big" for BigIntBigRat
In Wren exact currency calculations would normally be done in cents before converting the result to dollars and cents. For numbers of this size we need to use BigInt as 'normal' integers are limited to a maximum of 2^53 - 1.
<lang ecmascript>import "/big" for BigInt
 
var dollarsAndCentshamburgers = FnBigRat.new { |p|("4000000000000000")
var milkshakes var s = pBigRat.toStringtwo
var price1 = BigIntBigRat.newfromFloat(5505.5)
var dollars = (p/100).toString
var price2 = BigIntBigRat.newfromFloat(2862.86)
var cents = (p%100).toString
var taxPc = BigRat.fromFloat(0.0765)
return dollars + "." + cents
var totalPc = BigRat.fromFloat(1.0765)
}
var totalPreTax = hamburgers*price1 + milkshakes*price2 // in cents
 
var totalTax = taxPc * totalPreTax
var pcDollarsAndCents = Fn.new { |p, pc, mult|
var totalAfterTax = totalPreTax + totalTax
p = p * pc
System.print("Total price before tax : %(dollarsAndCents.call(totalPreTax).toDecimal(2))")
var div = BigInt.new(mult * 100)
System.print("Tax : %(pcDollarsAndCents(totalTax).calltoDecimal(totalPreTax, taxPc, 1e42))")
var dollars = (p/div).toString
System.print("Total price after tax : %(pcDollarsAndCents(totalAfterTax).calltoDecimal(totalPreTax, totalPc, 1e42))")</lang>
var cents = (p%div).toNum
cents = (cents/mult).round.toString
return dollars + "." + cents
}
 
var hamburgers = BigInt.new("4000000000000000")
var milkshakes = BigInt.two
var price1 = BigInt.new(550)
var price2 = BigInt.new(286)
var taxPc = BigInt.new(765) // 10,000 times too much
var totalPc = BigInt.new(10765) // ditto
var totalPreTax = hamburgers*price1 + milkshakes*price2 // in cents
System.print("Total price before tax : %(dollarsAndCents.call(totalPreTax))")
System.print("Tax : %(pcDollarsAndCents.call(totalPreTax, taxPc, 1e4))")
System.print("Total price after tax : %(pcDollarsAndCents.call(totalPreTax, totalPc, 1e4))")</lang>
 
{{out}}
9,486

edits