Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do the math sum of a text variable? (E.g 5865/100 )

I have a variable that is...

$whatever = "5865/100";

This is a text variable.

I want it to calculate 5865/100 , so that I can add it to other numbers and do a calculation.

Number_format doesn't work, as it just returns "5,865". Whereas I want it to return 58.65

I could do...

$explode=explode("/",$whatever);
if(count($explode)=="2") {
    $whatever = $explode[0]/$explode[1];
}

But it seems rather messy. Is there a simpler way?

like image 772
Tom Avatar asked Feb 06 '10 13:02

Tom


People also ask

What is the variable of sum?

The variable of summation is represented by an index which is placed beneath the summation sign. The index is often represented by i. (Other common possibilities for representation of the index are j and t.) The index appears as the expression i = 1.

What is the meaning of ∑?

The symbol Σ (sigma) is generally used to denote a sum of multiple terms. This symbol is generally accompanied by an index that varies to encompass all terms that must be considered in the sum. For example, the sum of first whole numbers can be represented in the following manner: 1 2 3 ⋯.

What is it called when you add 1 2 3 4 5?

Partial sums Numbers of this form are called triangular numbers, because they can be arranged as an equilateral triangle. The infinite sequence of triangular numbers diverges to +∞, so by definition, the infinite series 1 + 2 + 3 + 4 + ⋯ also diverges to +∞.

How do you write sums?

The Greek capital letter, ∑ , is used to represent the sum. The series 4+8+12+16+20+24 can be expressed as 6∑n=14n . The expression is read as the sum of 4n as n goes from 1 to 6 . The variable n is called the index of summation.


1 Answers

Evaluate as PHP expression, but first check if it contains only digits and operators and space, and suppress any errors.

if (preg_match('/^[\d\+\-\/\*\s]+$/', $s)) {
  @eval('$result = ' . $s . ';');
}
like image 59
Leventix Avatar answered Oct 05 '22 23:10

Leventix