Skip to content

Rounding

C272 edited this page Jul 3, 2019 · 3 revisions

In Algo, it's possible to round numbers to a given number of significant figures at any time. Say, for example, a user enters a float, and you want to print it to 4 significant figures. Here's how you'd do that, without any extra calculation:

import "io";
import "core";

//get some float
print "Enter a float!";
let someFlt = flt(input.get());

//print to 4 sf
print someFlt to 4 sf;

But what if you want to use this variable for calculations later on, but still want to round the value early, for whatever reason. Well, you can also do it when setting variables. Here's an example of that:

import "io";
import "core";

//get some float
print "Enter a float!";
let someFlt = flt(input.get());

//round the variable
someFlt = someFlt to 4 sf;

//do some maths
//...

This system also works if you use variables/expressions for the amount of figures you wish to round to (ie, it doesn't have to be a value). So, you can also ask the user how many significant figures they wish to round.

import "io";
import "core";

//get some float
print "Enter a float!";
let someFlt = flt(input.get());

//get the limit to round to
print "Enter how many significant figures to round to.";
let n = int(input.get());

//print to n sf
print someFlt to n sf;