What's the best way to add two values?

I have a form1 (from table1) that contains two fraction values (e.g. 1 1/4 and 3 5/8). These fields are declared as text fields because of the fractions.
In a second form2 (from table2) I have to make a sum of these two fractions. I’d like to do this automatically when I entered the two fractions in form1 and leave the second field.

Can this be done? I have no clue… Any ideas?

Joe

You can try with explode using space as delimiter

http://php.net/manual/es/function.explode.php

Thanks Giu, I googled about the explode function. But I have to do more search to find a code snippet for that problem.

Joe

I would convert the two fractions to floats … add them then convert them back

A bit code that converts a fraction to a float …

$input = ‘1 1/2’;
$fraction = array(‘whole’ => 0);
preg_match(’/^((?P<whole>\d+)(?=\s))?(\s*)?(?P<numerator>\d+)/(?P<denominator>\d+)$/’, $input, $fraction);
if (isset($fraction[‘denominator’]) ){
$result = $fraction[‘whole’] + $fraction[‘numerator’]/$fraction[‘denominator’];
print_r($result);die;
} else {
print_r($input);die;
}

And here the code to convert the float back to a fraction

function convert_DtoF($decimal){

$big_fraction = float2rat($decimal);
$num_array = explode('/', $big_fraction);
$numerator = $num_array[0];
$denominator = $num_array[1];
$whole_number = floor( $numerator / $denominator );
$numerator = $numerator % $denominator;

if($numerator == 0){
    return $whole_number;
}else if ($whole_number == 0){
    return $numerator . '/' . $denominator;
}else{
    return $whole_number . ' ' . $numerator . '/' . $denominator;
}

}

function float2rat($n, $tolerance = 1.e-6) {
$h1=1; $h2=0;
$k1=0; $k2=1;
$b = 1/$n;
do {
$b = 1/$b;
$a = floor($b);
$aux = $h1; $h1 = $a*$h1+$h2; $h2 = $aux;
$aux = $k1; $k1 = $a*$k1+$k2; $k2 = $aux;
$b = $b-$a;
} while (abs($n-$h1/$k1) > $n*$tolerance);

return "$h1/$k1";

}

printf("%s
", convert_DtoF(66.6666667));

Hi William, thank you very much. I’ll give it a try.

Joe

Hi, I first tested the first code snippet to convert a fraction into a float. Works fine as stand alone code in a php code tester but doesn’t work in SC properly.
If you set a fraction value as $input1 and $input2 it works but if you set an integer instead of a fraction SC stops calculating the two values.
Again in a php code tester it works with fractions and integers.
BTW: What data type should I use in SC for the fractions? If I use text I get a SQL error message when I update the record.

Any ideas?

Ajax - field_sum onFocus



$input1={field_fraction1};
$input2 ={field_fraction2};
$fraction = array('whole' => 0);
preg_match('/^((?P<whole>\d+)(?=\s))?(\s*)?(?P<numerator>\d+)\/(?P<denominator>\d+)$/', $input1, $fraction);
if (isset($fraction['denominator']) ){
$result1 = $fraction['whole'] + $fraction['numerator']/$fraction['denominator'];

}
$fraction = array('whole' => 0);
preg_match('/^((?P<whole>\d+)(?=\s))?(\s*)?(?P<numerator>\d+)\/(?P<denominator>\d+)$/', $input2, $fraction);
if (isset($fraction['denominator']) ){
$result2 = $fraction['whole'] + $fraction['numerator']/$fraction['denominator'];
}
{field_sum} = $result2+$result1;


Joe

Just check for the presence of a fraction before you call the function

Thank you William, but I think I must do the trick in a more simple way. I have no programming experience to solve the problem in your smart way. You have to know I don’t make my app for any commercial reason. I do it just in my freetime.

I was thinking about another solution. My situation is that in the first and second field I have only about 16 possible values to add. Let’s say in field one the values from 2 to 3 inches in 16th steps and in field two the values from 9 to 9 1/2 inches in 16th steps. I want to offer the user for every field a dropdown select where he can choose the possible values. After selection the sum should automatically show up in the sum field.
My idea was to create for the two single fields a table with one record for every possible value with the decimal conversion for each record. The same for the result. A table that contains every possible sum with the decimal conversion of the sum and then backwards comparing the decimal values and show up the summary of the two fields. Does anybody understand what I mean :wink: ?

I think this would be a dirty way to do this but my skills are limited :wink:

Has anybody an idea how to solve the problem that way?

William if you think I’m on the completely wrong way I would appreciate if you can give me more hints how I can customise your code to work with SC :slight_smile:

Joe