c# - Parse float, decimal or double -


i'm in process of rewriting our flat file parser, ungeneric, attribute oriented thingy more modern fluent synstax thingy. have problem coming way deal floating point numbers

given dto

public class foo  {    public decimal decimalvalue { get; set; } } 

and flatfile

1234561234 

you configure mapping this

mapper.createmap<foo>() .map(f => f.decimalvalue) .decimal(10, 4); //length 10, precision 4 

deserialize

var result = mapper<foo>.read(flatfile); 

it result in foo object property value 123456.1234

i have working code this, hardcoded decimal values only. i'm trying find generic way this. 1 problem there no generic constaint floating numbers.

the generic map method above example

    public propertymapresult<ttype, tproperty> map<tproperty>(expression<func<ttype, tproperty>> expression)     {         var result = new propertymapresult<ttype, tproperty>(this, expression);         propertymappers.add(result);         return result;     } 

the problem method, decimal, looks this

    public mapresult<ttype> decimal(int length, int precision)     {         this.length = length;         toformater = s => (tproperty)formatdecimal(s, precision);         fromformater = property => string.empty; //todo: fix writing of decimal, fix support double , float          return parent;     } 

and underlying formatdecimal

    private object formatdecimal(string data, int precision)     {         var integer = convertto<decimal>(data);         var frac = (decimal)math.pow(10, precision);          return integer/frac;     } 

one way overload of above methids, 1 each floating number type, not optimal. tried using dynamic casting formatdecimal method not help

any ideas?


Comments

Post a Comment

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -