Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7308423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:33:19+00:00 2026-05-28T23:33:19+00:00

I like to convert string with a price to a float value. The price

  • 0

I like to convert string with a price to a float value. The price comes from different languages and countries and can look like this:

 1,00 €
 € 1.00
 1'000,00 EUR
 1 000.00$
 1,000.00$
 1.000,00 EURO

or whatever you can think of…

Not sure I got the full range of possibilities with my examples. I am also not sure if it is possible to make in international convert blindly, maybe I have to use a language code? So for the start Euro and Dollar would be enough.

floatval() is kind of stupid so I need something more here. I think I should first remove all chars beside numbers, , and .. Then fix the , / . and use floatval finally.

Has someone done this before and can help me a little?

I would prefer a solution without regexp 😉

Update: Current solution with tests https://onlinephp.io/c/9e95e

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T23:33:20+00:00Added an answer on May 28, 2026 at 11:33 pm

    Ok, I tried it myself. What do you think of this?

    function priceToFloat($s){
        // is negative number
        $neg = strpos((string)$s, '-') !== false;
        
        // convert "," to "."
        $s = str_replace(',', '.', $s);
    
        // remove everything except numbers and dot "."
        $s = preg_replace("/[^0-9\.]/", "", $s);
    
        // remove all seperators from first part and keep the end
        $s = str_replace('.', '',substr($s, 0, -3)) . substr($s, -3);
    
        // Set negative number
        if( $neg ) {
            $s = '-' . $s;
        }
    
        // return float
        return (float) $s;
    }
    

    Here some tests: https://onlinephp.io/c/9e95e

    Sorry. I couldn’t include the other functions because codepad did not like them. But I compared them and there was trouble with strings like "22 000,76" or "22.000"

    Update: As Limitless isa pointed out you might have a look at the build in function money-format.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In MSSQL you can convert a string into an integer like this: CONVERT(INT, table.column)
I would like to convert this string foo_utf = u'nästy chäräctörs with å and
I'd like to convert a money formatted string into a float in javascript. ex:
can I convert a string to a html object? like: string s = '<div
I need to convert string like this: $url = 'module/controller/action/param1/param1value/paramX/paramXvalue'; to url regarding current
I'd like to convert a string to a float in C++. currently trying to
Trying to convert a string with some price 2 200,00 (without quotes) to look
Possible Duplicate: Convert string to float in Objective-C I'd like to convert a string
I would like to convert this string {id:1,name:Test1},{id:2,name:Test2} to array of 2 JSON objects.
I'd like to convert this string: $credit_packages_string = 300,0.25|1000,0.24|3000,0.22|4000,0.20|5000,0.18|6000,0.16|7000,0.14; into this array: $credit_packages =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.