c# - Decimal Truncating adding unlimited zeros -
c# - Decimal Truncating adding unlimited zeros -
i have entry field xamarin forms. have store value in decimal field.
and per requirement have have info in ##.#### format.
the validation should less hundred doing success.
however have issue when truncating problem.
my unfocused entry field follows.
private void rateentry_unfocused(object sender, focuseventargs e) { if (string.isnullorempty(((entry)sender).text)) { ((entry)sender).text = "0.00%"; _profileviewmodel.profile.rate = (decimal)0.00; } else { _profileviewmodel.profile.rate = math.truncate(convert.todecimal(((entry)sender).text)* 10000)/10000; ((entry)sender).text = addpercentagesymbol(_profileviewmodel.profile.rate); } validate(); } for illustration if give value 99.9999 , value 99.99990000000000000%
could please help me solve issue.
edit: function addpercentagesymbol
private string addpercentagesymbol(decimal value) { homecoming string.format("{0}{1}", value, "%"); } edit: expected outputs
99.9999 = 99.9999% 99.9999766 = 99.9999% 99.99 = 99.99% or 99.9900% 0.76433 = 0.7643%
i've reproduced - looks it's bug in mono. it's demonstrated:
decimal x = 9m; decimal y = x / 10; console.writeline(y); this should "0.9", it's "0.9000000000000000000000000000".
please study bug in mono :)
the news can utilize math.round rid of excess digits, e.g.
decimal z = math.round(y, 2); console.writeline(z); // 0.90 assuming multiplying 10000, truncating, , dividing 10000 round (down) 4 digits, should able away using math.round(value, 4) value won't have important digits after 4 decimal places anyway.
c# xamarin
Comments
Post a Comment