I have this string "45043". How can I convert it to decimal 450.43 (i want to use cultureinfo)
tks
string s = "45043";
decimal d = decimal.Parse(s) / 100;
(I'm not really sure where CultureInfo
comes into it. Do you want to convert the decimal
back into a string
or something like that?)
You'd have something like:
string x = "45043";
double num = Double.Parse(s, CultureInfo.GetCultureInfo("de-DE").NumberFormat);
Just replace with the culture you want or derive it dynamically.
Using CultureInfo? You mean using commas in certain cultures and periods in others?
How about:
var str = "45043";
var strToDecimal = (decimal.Parse(str) / 100).ToString();
var strExplicit = (decimal.Parse(str) / 100).ToString(System.Globalization.CultureInfo.CurrentCulture.NumberFormat); // more explicit version of what's happening above.
To show TryParse
and use of CultureInfo
string input = "45053";
decimal result;
if (decimal.TryParse(input, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.CurrentCulture, out result))
{
result /= 100M;
}
else
{
// invalid input, handle in whatever way you deem appropriate
}
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.