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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.