Logo

Programming-Idioms

Floor a decimal value, x, to the first significant digit.

For example, 1.0234 is approximately 1.02.

https://en.wikipedia.org/wiki/Significant_figures
New implementation

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
#include <cmath>
double i, n {modf(x, &i)},
       y {log(n) / log(.1)},
       d {pow(10, floor(y) + 1)};
x = trunc(x * d) / d;
let i = Math.trunc(x),
    n = x - i,
    y = Math.log(n) / Math.log(.1),
    d = Math.pow(10, Math.trunc(y) + 1)
x = Math.trunc(x * d) / d
import static java.lang.Math.log;
import static java.lang.Math.pow;
double i = (int) x,
       n = x - i,
       y = log(n) / log(.1),
       d = pow(10, (int) y + 1);
x = (int) (x * d) / d;
  str(x,s);
  p := Pos('.',s) + 1;
  while (s[p] = '0') do inc(p);
  setlength(s,p);
from decimal import Decimal
from math import floor, modf, log
num, integer = modf(x)
y = log(num) / log(Decimal('.1'))
denom = 10 ** (floor(y) + 1)
x = int(x * denom) / denom