简体   繁体   中英

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

This should work:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM