Public
Edited
Mar 10, 2023
Insert cell
Insert cell
d3.format(".2f")(0.00003 * 100)
Insert cell
d3.format(".0%")(0.1 + 0.2)
Insert cell
function formatPercentageNotNull(value) {
const MAX_DECIMALS = 3;
const MIN_VALUE_FOR_ONE_DECIMAL = 5;
let number;
let str;
let decimal = -1;

do {
decimal += 1;
number = d3.format(`.${decimal}f`)(Math.abs(value) * 100)
str = d3.format(`.${decimal}%`)(value)
} while (parseFloat(number) == 0 && decimal < MAX_DECIMALS)
const d = parseInt(d3.format(`.0f`)(Math.abs(value) * 100))
if (d <= MIN_VALUE_FOR_ONE_DECIMAL && d > 0) {
str = d3.format(`.1%`)(value)
}
return { str, number, value: parseFloat(number) };
}
Insert cell
formatPercentageNotNull(-0.0000003)
Insert cell
formatPercentageNotNull(-0.000003)
Insert cell
formatPercentageNotNull(-0.00003)
Insert cell
formatPercentageNotNull(0.0003)
Insert cell
formatPercentageNotNull(0.003)
Insert cell
formatPercentageNotNull(0.03)
Insert cell
formatPercentageNotNull(-0.03)
Insert cell
formatPercentageNotNull(0.3)
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more