Currency: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: added syntax colouring the hard way) |
|||
Line 978: | Line 978: | ||
</lang> |
</lang> |
||
=={{header|jq}}== |
|||
For simplicity, we will use the gojq implementation of jq |
|||
as it supports unbounded-precision integer arithmetic. |
|||
If the standard implementation of jq were used, |
|||
then it would probably be simplest to use the BigInt.jq library, |
|||
which uses strings for precision. |
|||
The strategy here is to use cents except when presenting |
|||
results as dollars and cents, and when calculating the taxes. |
|||
<lang jq>def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .; |
|||
# print as dollars and cents |
|||
def dollars: |
|||
(. % 100) as $c |
|||
| "$\((. - $c) /100).\($c)"; |
|||
def dollars($width): |
|||
dollars | lpad($width); |
|||
def innerproduct($y): |
|||
. as $x |
|||
| reduce range(0;$x|length) as $i (0; . + ($x[$i]*$y[$i])); |
|||
def plus($y): |
|||
. as $x |
|||
| reduce range(0;$x|length) as $i ([]; .[$i] = ($x[$i]+$y[$i])); |
|||
# Round up or down |
|||
def integer_division($y): |
|||
(. % $y) as $remainder |
|||
| (. - $remainder) / $y |
|||
| if $remainder * 2 > $y then . + 1 else . end; |
|||
# For computing taxes |
|||
def precision: 10000; |
|||
def cents: integer_division(precision); |
|||
### The task: |
|||
def p: [550, 286]; |
|||
def q: [4000000000000000, 2]; |
|||
def taxrate: 765; # relative to `precision` |
|||
(p | innerproduct(q)) as $before_tax # cents |
|||
| ($before_tax * taxrate) as $taxes # relative to precision |
|||
| ((($before_tax * precision) + $taxes) | cents) as $after_tax # cents |
|||
| ($after_tax|tostring|length + 2) as $width |
|||
| |
|||
" Total before tax: \($before_tax | dollars($width))", |
|||
" - tax: \($taxes | cents | dollars($width))", |
|||
" Total after tax: \($after_tax | dollars($width))"</lang> |
|||
{{out}} |
|||
<pre> |
|||
Total before tax: $22000000000000005.72 |
|||
- tax: $1683000000000000.44 |
|||
Total after tax: $23683000000000006.16 |
|||
</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
{{works with|Julia|1.2}} |
{{works with|Julia|1.2}} |
||