Jump to content

String to FLoat conversion


yoofy

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/205174-string-to-float-conversion/
Share on other sites

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.

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 :).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.