Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse a string to a float, without the currency format?

Tags:

string

c#

So i convert a float to a string, which is formatted as a currency.

float f = 2.99F;
string s = f.ToString("c2");
//s = 2.99 €

But when I want to convert it back to a float, it wouldn't be possible, because a float cant store the € symbol. So is there a way to convert the string back to a float, but it disregards the " €" (with the space)?

like image 716
Encore Avatar asked Mar 12 '14 19:03

Encore


1 Answers

This should work:

float f = 2.99F;
string s = f.ToString("c2");
var number = float.Parse(s, NumberStyles.AllowCurrencySymbol 
                           | NumberStyles.Currency);
like image 138
Selman Genç Avatar answered Oct 05 '22 12:10

Selman Genç