wright67uk Posted March 2, 2013 Share Posted March 2, 2013 How can I subtract from a variable, but keep my decimal places? When I try the below example I return 0 <?php $a = $_attributeSelection["backp1"]; echo $a -1; ?> // 1.53 becomes 0 However When I try the below example i return 0.69 <?php $b = 1.69; echo $b-1;?> // 1.69 becomes 0.69 Link to comment https://forums.phpfreaks.com/topic/275125-subtracting-and-keeping-decimals/ Share on other sites More sharing options...
Jessica Posted March 2, 2013 Share Posted March 2, 2013 Sounds like your first variable is a string. Do a var_dump on it to see. You can cast it to a float if needed. Link to comment https://forums.phpfreaks.com/topic/275125-subtracting-and-keeping-decimals/#findComment-1416013 Share on other sites More sharing options...
wright67uk Posted March 2, 2013 Author Share Posted March 2, 2013 Yes your right. Is floatval best used to do that? Link to comment https://forums.phpfreaks.com/topic/275125-subtracting-and-keeping-decimals/#findComment-1416027 Share on other sites More sharing options...
Barand Posted March 2, 2013 Share Posted March 2, 2013 Floatval() will not help with that field I'm looking to only return rows where the first two letters of $_attributeSelection["backp1"] are GB Link to comment https://forums.phpfreaks.com/topic/275125-subtracting-and-keeping-decimals/#findComment-1416033 Share on other sites More sharing options...
wright67uk Posted March 2, 2013 Author Share Posted March 2, 2013 That was used in a different file. In this circumstance I'm only using numbers. That was amazingly well spotted however!!! Link to comment https://forums.phpfreaks.com/topic/275125-subtracting-and-keeping-decimals/#findComment-1416036 Share on other sites More sharing options...
Barand Posted March 2, 2013 Share Posted March 2, 2013 What does var_dump() give then? Link to comment https://forums.phpfreaks.com/topic/275125-subtracting-and-keeping-decimals/#findComment-1416038 Share on other sites More sharing options...
wright67uk Posted March 2, 2013 Author Share Posted March 2, 2013 It says in using a string. Link to comment https://forums.phpfreaks.com/topic/275125-subtracting-and-keeping-decimals/#findComment-1416040 Share on other sites More sharing options...
Barand Posted March 3, 2013 Share Posted March 3, 2013 In that case you need to ... Link to comment https://forums.phpfreaks.com/topic/275125-subtracting-and-keeping-decimals/#findComment-1416097 Share on other sites More sharing options...
wright67uk Posted March 3, 2013 Author Share Posted March 3, 2013 Look at the manual, and get confused about the difference between floatval, intval and settype lol. I could try one of these, until one works, but I dont want to cause any problems later down the line by using the wrong one Link to comment https://forums.phpfreaks.com/topic/275125-subtracting-and-keeping-decimals/#findComment-1416125 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.