Old Russian measure of length: Difference between revisions
Content added Content deleted
m (→{{header|REXX}}: used a template for the output section.) |
SqrtNegInf (talk | contribs) (Added Perl example) |
||
Line 318: | Line 318: | ||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans| |
{{trans|Perl 6}} |
||
<lang d>import std.stdio, std.string, std.algorithm, std.conv; |
<lang d>import std.stdio, std.string, std.algorithm, std.conv; |
||
Line 912: | Line 912: | ||
Do another one y/n : n |
Do another one y/n : n |
||
</pre> |
</pre> |
||
=={{header|Perl}}== |
|||
Displaying converted values to 5 significant digits. |
|||
{{trans|Perl 6}} |
|||
<lang perl>sub convert { |
|||
my($magnitude, $unit) = @_; |
|||
my %factor = ( |
|||
tochka => 0.000254, |
|||
liniya => 0.00254, |
|||
diuym => 0.0254, |
|||
vershok => 0.04445, |
|||
piad => 0.1778, |
|||
fut => 0.3048, |
|||
arshin => 0.7112, |
|||
sazhen => 2.1336, |
|||
versta => 1066.8, |
|||
milia => 7467.6, |
|||
centimeter => 0.01, |
|||
meter => 1.0, |
|||
kilometer => 1000.0, |
|||
); |
|||
my $base= $magnitude * $factor{$unit}; |
|||
my $result .= "$magnitude $unit to:\n"; |
|||
for (sort { $factor{$a} <=> $factor{$b} } keys %factor) { |
|||
$result .= sprintf "%10s: %s\n", $_, sigdig($base / $factor{$_}, 5) unless $_ eq $unit |
|||
} |
|||
return $result; |
|||
} |
|||
sub sigdig { |
|||
my($num,$sig) = @_; |
|||
return $num unless $num =~ /\./; |
|||
$num =~ /([1-9]\d*\.?\d*)/; |
|||
my $prefix = $`; |
|||
my $match = $&; |
|||
my $digits = substr $match, 0, ++$sig; |
|||
my $nextd = substr $match, $sig, 1; |
|||
$digits =~ s/(.)$/{1+$1}/e if $nextd > 5; |
|||
return $prefix . $digits; |
|||
} |
|||
print convert(1,'meter'), "\n\n"; |
|||
print convert(1,'milia'), "\n";</lang> |
|||
{{out}} |
|||
<pre> tochka: 3937.0 |
|||
liniya: 393.70 |
|||
centimeter: 100 |
|||
diuym: 39.370 |
|||
vershok: 22.497 |
|||
piad: 5.6243 |
|||
fut: 3.2808 |
|||
arshin: 1.4061 |
|||
sazhen: 0.468691 |
|||
kilometer: 0.001 |
|||
versta: 0.000937383 |
|||
milia: 0.000133912 |
|||
1 milia to: |
|||
tochka: 29400000 |
|||
liniya: 2940000 |
|||
centimeter: 746760 |
|||
diuym: 294000 |
|||
vershok: 168000 |
|||
piad: 42000 |
|||
fut: 24500 |
|||
arshin: 10500 |
|||
meter: 7467.6 |
|||
sazhen: 3500 |
|||
kilometer: 7.4676 |
|||
versta: 7</pre> |
|||
=={{header|Perl 6}}== |
=={{header|Perl 6}}== |
||
Line 1,057: | Line 1,129: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
Follows the Perl solution, produces similar output. |
Follows the Perl 6 solution, produces similar output. |
||
<lang racket> |
<lang racket> |
||
#lang racket |
#lang racket |