Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert float to string with comma and dot

I have a nullable float. The internal decimal places can be separated with dot or comma e.g. 1.2 or 1,2. I need this float as a string to compare it to a Regex. If I use the Convert.toString method, the float with the comma is 12 and not 1.2. How can I convert a float to String without loosing the comma or the dot? I alredy tried to convert it with diffrent cultures.

Thanks for your help

like image 990
holzleube Avatar asked Jul 15 '26 03:07

holzleube


1 Answers

A solution for this can be the following:

float? num = 1.2f;
string floatAsString = string.Format("{0:f}", num.Value);

Maybe you need to check if the HasValue property is true before you use the value. For more examples: http://alexonasp.net/samples/stringformatting/

like image 69
tinu Avatar answered Jul 17 '26 19:07

tinu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!