After some days to research, I can't solve this problem. Then my partner suggests way to solve this problem as follow:
(1 * 10 * precision) / (reversedValue * 10 * precision)
Note: this way only applied for reversed value with precision > 0. For zero precision, we use normal way as 1 / reversedValue.
When working this task, I have another problem as 1 / 10000000 ==> 1e-7. We can't display this result to UI. So my partner and I found way to solve this problem as follow:
- Get exponent value and precision of number before 'e' to use as precision of result.
- Then use 'toFixed' function to convert result to number without exponent. If we use 'toFixed' function, we only have maximium precision 20.
Example: value = 1e-7;
value = value.toFixed(GetPrecision(value));
The following are functions to get reverse value and get precision (edit a function from researching on internet):
/// <summary> /// Get reverse value /// </summary> GetReverseValue: function (reversedValue) { var precision = 0; if (reversedValue === 0) { return; } reversedValue = value.toString().replace(/,/g, ""); reversedValue = parseFloat(reversedValue); precision = self.GetPrecision(reversedValue); if (precision > 0) { reversedValue = (1 * 10 * precision) / (reversedValue * 10 * precision); } else { reversedValue = 1 / reversedValue; reversedValue = reversedValue.toFixed(self.GetPrecision(reversedValue)); } return value; }, /// <summary> /// Get precision of a number /// </summary> GetPrecision: function (scinum) { if (typeof (scinum) !== "string") { scinum = scinum.toString(); } var arr = [], precision = 0; //get precision from exponent arr = scinum.split('e'); if (arr[1]) { precision = Math.abs(arr[1]); } //get precision from fraction arr = arr[0].split('.'); if (arr[1]) { precision += arr[1].length; } //if precision > 20, set it to max precision if (precision > 20) { precision = 20; } return precision; }
Không có nhận xét nào:
Đăng nhận xét