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
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
system.text.RegularExpressions.regex.match(inputstr,“[\d\s,]+“).tostring.replace(”,“,”.“).replace(” “,”")
cheers
Hey @abdel,
If your string is purely numbers you can use Cdbl(stringVar)
If the string is 17,456.34 EUR you can try
CDbl(System.Text.RegularExpressions.Regex.Match(StringVar,"(\d+(,?))*\d+(\.?)\d*").Value.ToString.Replace(",",""))
CDbl(System.Text.RegularExpressions.Regex.Match(StringVar,"\d+(\s?)\d+(\,?)\d*").Value.ToString.Replace(",",".").Replace(" ",""))
Hi @abdel
Use replace function
Input
Output
st.Replace(" ","").Replace(",",".").Replace("EUR","")
Thanks,
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
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.
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)
try this you will get the output
i did not see your suggestion !
Worked fine for me.
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.
we reworked on the pattern and forced it more strong to the french specific format of group separation by space char
\b(\d{1,3}( \d{3})*)(\,\d+)?\b
The double parsing we do as mentioned above
Double.Parse(YourStringVar,System.Globalization.CultureInfo.CreateSpecificCulture("fr-FR"))