significant-digits

Formatting numbers with significant figures in C#

不羁的心 提交于 2019-11-26 08:26:56
问题 I have some decimal data that I am pushing into a SharePoint list where it is to be viewed. I\'d like to restrict the number of significant figures displayed in the result data based on my knowledge of the specific calculation. Sometimes it\'ll be 3, so 12345 will become 12300 and 0.012345 will become 0.0123. Occasionally it will be 4 or 5. Is there any convenient way to handle this? 回答1: See: RoundToSignificantFigures by "P Daddy". I've combined his method with another one I liked. Rounding

Round a double to x significant figures

与世无争的帅哥 提交于 2019-11-26 03:57:49
问题 If I have a double (234.004223), etc., I would like to round this to x significant digits in C#. So far I can only find ways to round to x decimal places, but this simply removes the precision if there are any 0s in the number. For example, 0.086 to one decimal place becomes 0.1, but I would like it to stay at 0.08. 回答1: The framework doesn't have a built-in function to round (or truncate, as in your example) to a number of significant digits. One way you can do this, though, is to scale your

Rounding to an arbitrary number of significant digits

為{幸葍}努か 提交于 2019-11-26 02:54:46
问题 How can you round any number (not just integers > 0) to N significant digits? For example, if I want to round to three significant digits, I\'m looking for a formula that could take: 1,239,451 and return 1,240,000 12.1257 and return 12.1 .0681 and return .0681 5 and return 5 Naturally the algorithm should not be hard-coded to only handle N of 3, although that would be a start. 回答1: Here's the same code in Java without the 12.100000000000001 bug other answers have I also removed repeated code,