Questions tagged [significant-digits]

Significant digits are a way of describing the precision of measurements in a scale-independent way.

Typically if a number is in normal scientific notation, the number of significant digits is the length (number of digits) of the mantissa or significand. The significant figures of a number are those digits that carry meaning contributing to its precision. This includes all digits except: All leading zeros, Trailing zeros when they are merely placeholders to indicate the scale of the number (exact rules are explained at Identifying significant figures), and spurious digits introduced, for example, by calculations carried out to greater precision than that of the original data, or measurements reported to a greater precision than the equipment supports.

Numbers are often rounded to avoid reporting insignificant figures. For instance, if a device measures to the nearest gram and gives a reading of 12.345 kg, it would create false precision to express this measurement as 12.34500 kg. Significant Figures - Wikipedia

178 questions
209
votes
26 answers

How to round a number to significant figures in Python

I need to round a float to be displayed in a UI. e.g, to one significant figure: 1234 -> 1000 0.12 -> 0.1 0.012 -> 0.01 0.062 -> 0.06 6253 -> 6000 1999 -> 2000 Is there a nice way to do this using the Python library, or do I have to write it…
Peter Graham
  • 11,323
  • 7
  • 40
  • 42
87
votes
17 answers

Rounding to an arbitrary number of significant digits

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…
DougN
  • 4,407
  • 11
  • 56
  • 81
81
votes
17 answers

Round a double to x significant figures

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…
Rocco
  • 1,395
  • 3
  • 11
  • 19
39
votes
10 answers

Is floating point precision mutable or invariant?

I keep getting mixed answers of whether floating point numbers (i.e. float, double, or long double) have one and only one value of precision, or have a precision value which can vary. One topic called float vs. double precision seems to imply that…
36
votes
3 answers

Show decimal places and scientific notation on the axis

I am plotting some big numbers with matplotlib in a pyqt program using python 2.7. I have a y-axis that ranges from 1e+18 to 3e+18 (usually). I'd like to see each tick mark show values in scientific notation and with 2 decimal places. For example…
tempneff
  • 365
  • 1
  • 3
  • 4
32
votes
9 answers

Formatting numbers with significant figures in C#

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…
Chris Farmer
  • 24,974
  • 34
  • 121
  • 164
24
votes
3 answers

Is the most significant decimal digits precision that can be converted to binary and back to decimal without loss of significance 6 or 7.225?

I've come across two different precision formulas for floating-point numbers. ⌊(N-1) log10(2)⌋ = 6 decimal digits (Single-precision) and N log10(2) ≈ 7.225 decimal digits (Single-precision) Where N = 24 Significant bits (Single-precision) The…
24
votes
5 answers

Round to n Significant Figures in SQL

I would like to be able to round a number to n significant figures in SQL. So: 123.456 rounded to 2sf would give 120 0.00123 rounded to 2sf would give 0.0012 I am aware of the ROUND() function, which rounds to n decimal places rather than…
Paul
  • 16,285
  • 13
  • 41
  • 52
17
votes
3 answers

Is there a way to get the "significant figures" of a decimal?

Update OK, after some investigation, and thanks in big part to the helpful answers provided by Jon and Hans, this is what I was able to put together. So far I think it seems to work well. I wouldn't bet my life on its total correctness, of…
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
13
votes
3 answers

Round down a numeric

I have numeric's like this one: a <- -1.542045 And I want to round them down (or round up the abs) to 2 digits after the decimal point. signif(a,3) will round it down and give me 1.54 as a result but for this example the result I want is…
dan
  • 6,048
  • 10
  • 57
  • 125
13
votes
4 answers

Nicely representing a floating-point number in python

I want to represent a floating-point number as a string rounded to some number of significant digits, and never using the exponential format. Essentially, I want to display any floating-point number and make sure it “looks nice”. There are several…
dln385
  • 11,630
  • 12
  • 48
  • 58
12
votes
7 answers

How do I round a float to a specified number of significant digits in Ruby?

It would be nice to have an equivalent of R's signif function in Ruby. For example: >> (11.11).signif(1) 10 >> (22.22).signif(2) 22 >> (3.333).signif(2) 3.3 >> (4.4).signif(3) 4.4 # It's usually 4.40 but that's OK. R does not print the trailing 0's …
Aleksandr Levchuk
  • 3,751
  • 4
  • 35
  • 47
12
votes
6 answers

How can I round to an arbitrary number of significant digits with JavaScript?

I tried below sample code function sigFigs(n, sig) { if ( n === 0 ) return 0 var mult = Math.pow(10, sig - Math.floor(Math.log(n < 0 ? -n: n) / Math.LN10) - 1); return Math.round(n * mult) / mult; } But this function…
suresh inakollu
  • 158
  • 1
  • 1
  • 6
11
votes
10 answers

How to get excel to display a certain number of significant figures?

I am using excel and i want to display a value to a certain number of significant figures. I tried using the following equation =ROUND(value,sigfigs-1-INT(LOG10(ABS(value)))) with value replaced by the number I am using and sigfigs replaced with…
Veridian
  • 3,531
  • 12
  • 46
  • 80
10
votes
2 answers

Specify the number of decimal digits on the explicit generator of a sequence in Raku

I've written a simple code: sub euler-ex ($x) of Seq { 1, { $x**++$ / [×] 1 .. ++$ } ... Inf } say " 5: " ~ euler-ex(5)[^20] ~ " = " ~ [+](euler-ex(5)[^20]); The output: 5: 1 5 12.5 20.833333 26.041667 26.041667 21.701389 15.500992 9.68812…
Lars Malmsteen
  • 738
  • 4
  • 23
1
2 3
11 12