Currency: Difference between revisions

1,471 bytes added ,  3 years ago
Added Wren
(Added Wren)
Line 1,503:
Tax € 1.683.000.000.000.000,44
Total with tax € 23.683.000.000.000.006,16</pre>
 
=={{header|Wren}}==
{{libheader|Wren-big}}
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 dollarsAndCents = Fn.new { |p|
var s = p.toString
var dollars = (p/100).toString
var cents = (p%100).toString
return dollars + "." + cents
}
 
var pcDollarsAndCents = Fn.new { |p, pc, mult|
p = p * pc
var div = BigInt.new(mult * 100)
var dollars = (p/div).toString
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}}
<pre>
Total price before tax : 22000000000000005.72
Tax : 1683000000000000.44
Total price after tax : 23683000000000006.16
</pre>
 
=={{header|zkl}}==
9,477

edits