App-FoodAdditivesUtils

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN


         convert_benzoate_unit(quantity => "ppm");

        Result:

         [
           200,
           "OK",
           [
             { amount => 1, unit => "ppm" },
             { amount => 0.847408229824443, unit => "ppm-as-benzoic-acid" },
             { amount => 1, unit => "ppm-as-sodium-benzoate" },
             { amount => 1, unit => "ppm-as-na-benzoate" },
             { amount => 1.11172021372563, unit => "ppm-as-potassium-benzoate" },
             { amount => 1.04239816806606, unit => "ppm-as-k-benzoate" },
             { amount => 0.979494830337937, unit => "ppm-as-calcium-benzoate" },
             { amount => 0.979494830337937, unit => "ppm-as-ca-benzoate" },
           ],
           {},
         ]

    *   Convert from ppm (as sodium benzoate) to ppm (as benzoic acid):

         convert_benzoate_unit(quantity => "250 ppm", to_unit => "ppm-as-benzoic-acid");

        Result:

         [200, "OK", 211.852057456111, {}]

    If target unit is not specified, will show all known conversions.

    This function is not exported.

    Arguments ('*' denotes required arguments):

lib/App/FoodAdditivesUtils.pm  view on Meta::CPAN

    description => <<'_',

If target unit is not specified, will show all known conversions.

_
    args => {
        %args_common,
    },
    examples => [
        {args=>{quantity=>'ppm'}, summary=>'Show all possible conversions'},
        {args=>{quantity=>'250 ppm', to_unit=>'ppm-as-benzoic-acid'}, summary=>'Convert from ppm (as sodium benzoate) to ppm (as benzoic acid)'},
    ],
};
sub convert_benzoate_unit {
    require Physics::Unit;

    Physics::Unit::InitUnit(
        ['ppm'], '1 mg/kg',
        ['ppm-as-benzoic-acid'], '1.18006878480183 mg/kg', # benzoic acid's molecular weight = 122.12, sodium benzoate's molecular weight = 144.11
        ['ppm-as-sodium-benzoate'], '1 mg/kg',
        ['ppm-as-na-benzoate'], '1 mg/kg',
        ['ppm-as-potassium-benzoate'], '0.899506897197428 mg/kg', # potassium benzoate's molecular weight = 160.21
        ['ppm-as-k-benzoate'], '0.959326321395287 mg/kg',
        ['ppm-as-calcium-benzoate'], '1.02093443377847 mg/kg', # calcium benzoate's molecular weight = 282.31 (2 benzoate groups per molecule)
        ['ppm-as-ca-benzoate'], '1.02093443377847 mg/kg',
    );

    my %args = @_;
    my $quantity = Physics::Unit->new($args{quantity});
    return [412, "Must be a Dimensionless quantity"] unless $quantity->type eq 'Dimensionless';

    if ($args{to_unit}) {
        my $new_amount = $quantity->convert($args{to_unit});
        return [200, "OK", $new_amount];
    } else {
        my @rows;
        for my $u (
            'ppm',
            'ppm-as-benzoic-acid',
            'ppm-as-sodium-benzoate',
            'ppm-as-na-benzoate',
            'ppm-as-potassium-benzoate',
            'ppm-as-k-benzoate',
            'ppm-as-calcium-benzoate',
            'ppm-as-ca-benzoate',
        ) {
            push @rows, {
                unit => $u,
                amount => $quantity->convert($u),

lib/App/FoodAdditivesUtils.pm  view on Meta::CPAN


 convert_benzoate_unit(quantity => "ppm");

Result:

 [
   200,
   "OK",
   [
     { amount => 1, unit => "ppm" },
     { amount => 0.847408229824443, unit => "ppm-as-benzoic-acid" },
     { amount => 1, unit => "ppm-as-sodium-benzoate" },
     { amount => 1, unit => "ppm-as-na-benzoate" },
     { amount => 1.11172021372563, unit => "ppm-as-potassium-benzoate" },
     { amount => 1.04239816806606, unit => "ppm-as-k-benzoate" },
     { amount => 0.979494830337937, unit => "ppm-as-calcium-benzoate" },
     { amount => 0.979494830337937, unit => "ppm-as-ca-benzoate" },
   ],
   {},
 ]

=item * Convert from ppm (as sodium benzoate) to ppm (as benzoic acid):

 convert_benzoate_unit(quantity => "250 ppm", to_unit => "ppm-as-benzoic-acid");

Result:

 [200, "OK", 211.852057456111, {}]

=back

If target unit is not specified, will show all known conversions.

This function is not exported.

script/convert-benzoate-unit  view on Meta::CPAN


=head1 EXAMPLES

=head2 Show all possible conversions

 % convert-benzoate-unit ppm
 +-------------------+---------------------------+
 |            amount |                      unit |
 +-------------------+---------------------------+
 |                 1 |                       ppm |
 | 0.847408229824443 |       ppm-as-benzoic-acid |
 |                 1 |    ppm-as-sodium-benzoate |
 |                 1 |        ppm-as-na-benzoate |
 |  1.11172021372563 | ppm-as-potassium-benzoate |
 |  1.04239816806606 |         ppm-as-k-benzoate |
 | 0.979494830337937 |   ppm-as-calcium-benzoate |
 | 0.979494830337937 |        ppm-as-ca-benzoate |
 +-------------------+---------------------------+

=head2 Convert from ppm (as sodium benzoate) to ppm (as benzoic acid)

 % convert-benzoate-unit '250 ppm' ppm-as-benzoic-acid
 211.852057456111

=head1 HOMEPAGE

Please visit the project's homepage at L<https://metacpan.org/release/App-FoodAdditivesUtils>.

=head1 SOURCE

Source repository is at L<https://github.com/perlancar/perl-App-FoodAdditivesUtils>.



( run in 1.549 second using v1.01-cache-2.11-cpan-de7293f3b23 )