c# - trying to create a extension method to convert from string to decimal -


i'm trying come global way of converting string x number of decimal places , i'm having no luck. need return decimal x number of decimals.

here have far cannot figure out how know divide easily:

public static decimal toxdecimalplaces(this object value, int numberofdecimalplaces, decimal defaultvalue = 0) {   double retval;   if (double.tryparse(value.tostring(), out retval))   {     return (decimal)(retval / 10);   }   return defaultvalue; } 

so if send it:

value = "12345" value.toxdecimalplaces(2)  

i'd back:

123.45 

etc.

the division of retval needs different pending on numberofdecimalplaces.

any suggestions? prefer not have create handful of extension methods or should do?

should create:

to1decimalplaces to2decimalplaces to3decimalplaces etc 

for each 1 need , move on?

how using math.pow?

something like

return (decimal)(retval / math.pow(10, numberofdecimalplaces)); 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -