hi,

i'm making a website for a company, with backoffice. when i want to add a new employee, i must specify the monthly fee and i created two inputs, one for the dollar part and the other for the cents parts. i wanted to concatenate the two parts with a dot in the middle and then cast to float. but when i insert the employee, the salary stays on 99.99

what can i do?

Provide some code.

$euros = $_POST['euros'];
$cents = $_POST['cents'];

$salary = $euros.".".$cents;
$salaryFloat = (float) $salary;
$euros = $_POST['euros'];
$cents = $_POST['cents'];
if(is_numeric($euros) && is_numeric($cents)) { $salary = $euros + ($cents / 100); }
else die('non-numeric inputs');

oneline code

isnumeric($_POST['euros']) && isnumeric($_POST['cents']) ? $salary=$_POST['euros']+($_POST['cents']/100):die('non-numeric inputs');

salaryfl;oat appears unneccessary

Member Avatar for diafol

is_int() better AB? is_numeric() will accept float (and hex?) as input.
Can just imagine some noob entering .89 for 89c as opposed to just 89

commented: Thanks bloke +13

and the isnumeric() in the last one-liner should have been is_numeric(), is_int() is better

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.