Skip to content
Find file
45 lines (36 sloc) 997 Bytes
package DDG::Goodie::Base;
# ABSTRACT: convert numbers between arbitrary bases
use strict;
use DDG::Goodie;
use Math::Int2Base qw/int2base/;
use 5.010;
zci answer_type => "conversion";
zci is_cached => 1;
use bigint;
my %base_map = (
hex => 16,
hexadecimal => 16,
oct => 8,
octal => 8,
binary => 2,
);
my $map_keys = join '|', keys %base_map;
triggers any => 'base', keys %base_map;
handle query_clean => sub {
return unless /^(?<num>[0-9]+)\s*(?:(?:in|as|to)\s+)?(?:(?<bt>$map_keys)|(?:base\s*(?<bn>[0-9]+)))$/;
my $number = $+{'num'};
my $base = $+{'bn'} // $base_map{$+{bt}};
return if $base < 2 || $base > 36;
my $based = int2base($number, $base);
return "$number in base $base is $based",
structured_answer => {
data => {
title => $based,
subtitle => "Decimal to base $base: $number"
},
templates => {
group => 'text'
}
};
};
1;
Something went wrong with that request. Please try again.