We have an interesting problem that we need to determine the decimal accuracy of user input (textbox). Essentially we need to know the number of decimal places and then need to return an exact number, this is the best example with examples:
4500 will get the results recorded 1 # 4500.1 Results of the result recorded 0.1 <4500.00 results will result in 0.01
recorded 4500.450 results will result in 0.001
We are thinking to work with the string, find the decimal separator and then calculate the result. Just wondering that its solution is an easy solution.
I think the suggestion that you use, the point of use of the decimal point is a clear flaw You may have to think of internationalization yourself.
var decimal separator = number format inf ... Current Infoc Var position = input.indexOf (decimal separator); Var exact = (position == -1)? 0: Input Long - Position - 1; // It can be quite reasonably var result = Math Po (0.1, exact); There is one more thing that you can try - the decimal type stores an internal exact value. So you can use the deciml.TryParse () and observe the returned value. The parsing algorithm may maintain the accuracy of the input.
Finally, I suggest that do not try to use floating point numbers. Parsing the input will delete any information that is behind zero. So you have to add an artificial non-zero point to maintain them or make a similar era. You can run into precise issues, in the end, it is not easy to find exact based on a floating point number too. I see some ugly math or loop looking at every one of the ten times running, until there is no partial part. And the loop comes with new precision issues ...
UPDATE
Look for parsing details in a DCML function.
var input = "123.4560"; Var number = decimal Purse (input); // will be 4. var precision = (decimal. Gatebits (number) [3]> gt; & gt; 16) & amp; 0x000000FF; Here's directly using Math.Pow (0.1, exact).
Comments
Post a Comment