c# - Convert string to decimal with format -
i need convert string decimal in c#, string have different formats.
for example:
"50085"
"500,85"
"500.85"
this should convert 500,85 in decimal. there simplified form convertion using format?
while decimal.parse() method looking for, have provide bit more information it. not automatically pick between 3 formats give, have tell format expecting (in form of iformatprovider). note iformatprovider, don't think "50085" pulled in.
the consistent thing see appears examples expect 2 decimal places of precision. if case, strip out periods , commas , divide 100.
maybe like:
public decimal? customparse(string incomingvalue) { decimal val; if (!decimal.tryparse(incomingvalue.replace(",", "").replace(".", ""), numberstyles.number, cultureinfo.invariantculture, out val)) return null; return val / 100; }
Comments
Post a Comment