Skip to content

This site describes solve-engine 2.44.0, the version npm installs.

Rounding

Package: FUNCTION_PACKAGE. Registered by createEngine(); for a slimmer engine, register it explicitly (see choosing packages).

Rounding replaces a number with a nearby simpler one: the nearest whole number, a set number of decimal places, or the nearest ten, hundred or thousand. Solve has a form for each, in symbols and in words, so you can say the one you mean.

round on its own goes to the nearest whole number. Give it a place count, or say to N dp, to round to that many decimal places and show exactly that many, trailing zeros kept.

round(3.7) // 4
round(3.14159, 2) // 3.14
3.14159 to 4 dp // 3.1416
1.5 to 2 dp // 1.50
100 to 2 dp // 100.00

The place count is a precision you set on the value, not a global display setting, so a rounded number reads the way you asked and carries that precision into the next line. The rounding is exact where the number has an exact decimal, so a half at the last place goes up rather than down.

1.005 to 2 dp // 1.01
round(2.675, 2) // 2.68

A number exactly halfway between two others rounds away from zero, however the rounding is written: 2.5 is 3, and -2.5 is -3. That is the rule a spreadsheet’s ROUND follows, and it keeps a rounding symmetric, so a negative amount rounds to the negative of what its positive would. rounded up and rounded down name a direction instead, and follow it. A result that rounds to zero is written as 0, without a sign.

round(2.5) // 3
round(-2.5) // -3
-2.5 rounded // -3
-2.5 rounded up // -2
-25 to nearest 10 // -30
-0.4 rounded // 0

Significant figures count from the first digit that is not zero, which is how a measured value is reported: a reading of 0.0012345 known to two figures is 0.0012, and a population of 1,234,567 to three figures is 1,230,000. to N sf rounds to that many figures, and shows a trailing zero that is one of them, as to N dp does. sig figs and significant figures are the long spellings.

1234567 to 3 sf // 1,230,000
0.0012345 to 2 sf // 0.0012
2.5 to 3 sf // 2.50
9.99 to 2 sf // 10
5.678 km to 2 sf // 5.7 km

The figure count runs from 1 to 17, the most a double carries.

Rounding to a magnitude reads the way it is said. rounded with no target is the nearest whole; up and down force the direction; to nearest <n> rounds to a multiple, and the round magnitude words (ten, hundred, thousand, …) stand in for the number.

5.5 rounded // 6
5.4 rounded up // 6
5.6 rounded down // 5
37 to nearest 10 // 40
490 rounded to nearest hundred // 500
21 rounded up to nearest 5 // 25

A list, such as [0.001, 0.006] or what map gives back, holds several numbers, and rounding it rounds each one on its own. The list keeps its length, its order and its unit, and each number in it is rounded and shown as the same number would be on a line of its own, trailing zeros and all. Every form on this page works this way, and so do ceil, floor and int.

[0.001, 0.006] to 4 dp // [0.0010, 0.0060]
map(x/1000, 1:3) to 4 dp // [0.0010, 0.0020, 0.0030]
round([1.5, 2.4]) // [2, 2]
[1.005, 2.675] to 2 dp // [1.01, 2.68]
[1234, 0.5] to 2 sf // [1,200, 0.50]
[12, 37] to nearest 10 // [10, 40]
[1 km, 2.345 km] to 1 dp // [1.0 km, 2.3 km]

A list has no single number, and these used to read it as 0 and answer one zero: [0.001, 0.006] to 4 dp was 0.0000. A number in a list is rounded from the decimal it is written as, so 1.005 in a list goes up to 1.01 as it does on its own.

The boundary: a conversion that writes a value as one number, in scientific notation, as a fraction or a percentage, in hex, binary or octal, or as number, has no list form, since the text it writes is one number. Each refuses a list by name rather than answer for a 0 it read in its place; convert one value at a time. A list of true and false has nothing to round, and is refused too.

[1234, 5678] as sci // A list cannot be written in scientific notation: it holds several numbers, not one. Convert one value at a time.
[0.5, 0.25] as % // A list cannot be written as a percentage: it holds several numbers, not one. Convert one value at a time.
[true, false] to 2 dp // A list can be rounded only when every cell is a number: this one holds a true or false.