yoofy Posted June 18, 2010 Share Posted June 18, 2010 Hi, I am having some trouble with the String->Float conversion. I have a currentProduct class that I call when importing products. The thing is I need to import placementPrice which is a float, ex.: 10.45 . When I first import the price, it says 10,45 as a string. I want to convert it to 10.45 (float) and then assign it to the placementPrice in CurrentProduct but it won't let me. When I str_replace(",",".",$placementPrice) then floatval($PlacementPrice) I loose all the decimal value, ex.: 10.45 -> 10. I would like to be able to keep all the 10.45. Any of you know how? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/205174-string-to-float-conversion/ Share on other sites More sharing options...
Daniel0 Posted June 18, 2010 Share Posted June 18, 2010 $str = '10,45'; $str = str_replace(',', '.', $str); var_dump(floatval($str)); gives float(10.45) as expected. You'll have to show some code. Quote Link to comment https://forums.phpfreaks.com/topic/205174-string-to-float-conversion/#findComment-1073935 Share on other sites More sharing options...
yoofy Posted June 18, 2010 Author Share Posted June 18, 2010 My error is no longer in the conversion from string to float. It is now when I put my float value in the paramater ex.: $Myclass = new currentProduct('x','y',(float here), 'z') When I load it in the constructer of CurrentProduct, it still says is_string to true. Quote Link to comment https://forums.phpfreaks.com/topic/205174-string-to-float-conversion/#findComment-1073938 Share on other sites More sharing options...
yoofy Posted June 18, 2010 Author Share Posted June 18, 2010 Plus, not only it is a string when I get into the constructer, but it has deleted all the decimal value, so I get a string of 10.00 instead of a float of 10.45. Quote Link to comment https://forums.phpfreaks.com/topic/205174-string-to-float-conversion/#findComment-1073943 Share on other sites More sharing options...
Daniel0 Posted June 18, 2010 Share Posted June 18, 2010 You'll have to show some code. Quote Link to comment https://forums.phpfreaks.com/topic/205174-string-to-float-conversion/#findComment-1073960 Share on other sites More sharing options...
yoofy Posted June 18, 2010 Author Share Posted June 18, 2010 Well, I found a solution to my problem. Since it was string that my constructor received. What I did was multiply by 100 before passing the parameter. So the 10.45 became 1045, then the 1045 became a string. In my constructor I took my (string)1045 and floatval'ed it and then divided it by 100 so I got 10,45. THanks anyway . Quote Link to comment https://forums.phpfreaks.com/topic/205174-string-to-float-conversion/#findComment-1073966 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.