Extract double from a string

Hello friend,

I’m trying to extract double from a string:

for exemple “17 456,34 EUR” => 17456.34

wahat will be the best actions to do that with regex

@abdel

system.text.RegularExpressions.regex.match(inputstr,“[\d\s,]+“).tostring.replace(”,“,”.“).replace(” “,”")

cheers

Hey @abdel,

  1. If your string is purely numbers you can use Cdbl(stringVar)

  2. If the string is 17,456.34 EUR you can try

CDbl(System.Text.RegularExpressions.Regex.Match(StringVar,"(\d+(,?))*\d+(\.?)\d*").Value.ToString.Replace(",",""))
  1. If the string is 17 456,34 EUR you can try
CDbl(System.Text.RegularExpressions.Regex.Match(StringVar,"\d+(\s?)\d+(\,?)\d*").Value.ToString.Replace(",",".").Replace(" ",""))

Hi @abdel

Use replace function

Input

image

Output
image

st.Replace(" ","").Replace(",",".").Replace("EUR","")

Thanks,

grafik

first line we use regex to grab the value
second line we parse the string into a double. As French formats can use space for group seperation it is handled by using the dedicated CultureInfo

Hi @abdel

strvar= "17 456,34"
output=strvar.Replace(" ","").Replace(",",".")

Regards,

Regex won’t give you a double, though. It gives you a string.

Replace to remove the space and convert comma to period (conversion may be unnecessary in your environment). RegEx to get just digits, comma and period. CDbl to convert to double. “F2” to always get two decimal places (if you really want a double, take off the .ToString(“F2”))

myStr = “17 456,34 EUR”

CDbl(System.Text.RegularExpressions.RegEx.Match(myStr.Replace(" ","").Replace(",","."),"\d+(?:[,.]\d+)*").ToString).ToString("F2")

This will work dynamically and is not as dependent on the string being exactly as the above like the other solutions.

not working :slight_smile:

Hi @abdel

Please try this

Regex1.Replace(" ", "").Replace(",", ".")

Hope this helps!!

Assuming you didn’t use the .ToString(“F2”) on the end, it did work. A value of 25.30 will be 25.3 as a double. It’s the .ToString(“F2”) that gives you a string formatted to two decimal places.

Hi,

Try this

The pattern \d+\s?\d+,\d+ will match numbers with spaces or commas.

did not work: for exemple for a string “1 510,18 EUR”, it gives 1 as a double

the code is attached
FormatStringEnDouble.xaml (12.9 KB)

@abdel

try this you will get the output

i did not see your suggestion !

@abdel

use the syntax which @ppr as given it is working fine

Worked fine for me.

image

CDbl(System.Text.RegularExpressions.RegEx.Match(myStr.Replace(" ","").Replace(",","."),"\d+(?:[,.]\d+)*").ToString)

it did not work with the value “0,00 EUR”

It’s possible that because of your localization it uses the comma as the decimal separator automatically, and replacing it with a period is the problem.

Try it without replacing the comma with a period:

CDbl(System.Text.RegularExpressions.RegEx.Match(myStr.Replace(" ",""),"\d+(?:[,.]\d+)*").ToString)

Works for me, again your localization is probably the difference for us. Try it without replacing the comma.

@abdel

we reworked on the pattern and forced it more strong to the french specific format of group separation by space char

grafik

\b(\d{1,3}( \d{3})*)(\,\d+)?\b

The double parsing we do as mentioned above
grafik

Double.Parse(YourStringVar,System.Globalization.CultureInfo.CreateSpecificCulture("fr-FR"))