Encore
Encore

Reputation: 275

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

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)?

Upvotes: 5

Views: 531

Answers (1)

Selman Genç
Selman Genç

Reputation: 101681

This should work:

float f = 2.99F;
string s = f.ToString("c2");
var number = float.Parse(s, NumberStyles.AllowCurrencySymbol 
                           | NumberStyles.Currency);

Upvotes: 8

Related Questions