Module 1 · Mathematical Language and Quantitative Reasoning Lesson 10 of 120
Rounding, Precision, Tolerance, and Significant Digits
Rounding a half-cent without guessing binary behavior.
Transcript
23 sentences · select one to jump thereCheck your understanding
Can this nonnegative half-up snippet be reused unchanged for negative amounts?
Code lab
Run it yourself
The lesson source in 7 languages. Edit it, run TypeScript and Python right here, and compare with the expected output.
/**
* Fintech Math Bootcamp · Lesson 010 of 120
* Rounding, Precision, Tolerance, and Significant Digits
* Module 01: Mathematical Language and Quantitative Reasoning
*
* Scenario: Rounding a half-cent without guessing binary behavior
* Rule: half-up(10.005) = 10.01 under a decimal policy
*
* Try it: Can this nonnegative half-up snippet be reused unchanged for negative amounts?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/mathematical-language-and-quantitative-reasoning/rounding-precision-tolerance-and-significant-digits/
* Free course: https://courses.thefintechbuilder.com
* Synthetic teaching example, not financial advice or a production library.
*/
export function lesson010() {
const thousandths = 10005; // exact decimal input units
if (!Number.isInteger(thousandths) || thousandths < 0)
throw new Error("Nonnegative integer thousandths only");
const cents = Math.floor((thousandths + 5) / 10); // half-up
const result = cents / 100; // 10.01
return result;
}
export const checkedResult = 10.01;
// Run this file directly: npx tsx lessons/01-mathematical-language-and-quantitative-reasoning/010-rounding-precision-tolerance-and-significant-digits.ts
if (process.argv[1] && import.meta.url.endsWith(process.argv[1].replace(/\\/g, "/").split("/").pop()!)) {
console.log(JSON.stringify(lesson010(), null, 2));
}
Your output
Press Run to execute the code in your browser.
Expected output
10.01"""
Fintech Math Bootcamp · Lesson 010 of 120
Rounding, Precision, Tolerance, and Significant Digits
Module 01: Mathematical Language and Quantitative Reasoning
Scenario: Rounding a half-cent without guessing binary behavior
Rule: half-up(10.005) = 10.01 under a decimal policy
Try it: Can this nonnegative half-up snippet be reused unchanged for negative amounts?
Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/mathematical-language-and-quantitative-reasoning/rounding-precision-tolerance-and-significant-digits/
Free course: https://courses.thefintechbuilder.com
Synthetic teaching example, not financial advice or a production library.
Run it: python main.py
"""
import json
def lesson010() -> float:
thousandths = 10005 # exact decimal input units
if not isinstance(thousandths, int) or thousandths < 0:
raise ValueError("Nonnegative integer thousandths only")
cents = (thousandths + 5) // 10 # half-up
result = cents / 100 # 10.01
return result
if __name__ == "__main__":
print(json.dumps(lesson010(), indent=2))
Your output
Press Run to execute the code in your browser.
Expected output
10.01/**
* Fintech Math Bootcamp · Lesson 010 of 120
* Rounding, Precision, Tolerance, and Significant Digits
* Module 01: Mathematical Language and Quantitative Reasoning
*
* Scenario: Rounding a half-cent without guessing binary behavior
* Rule: half-up(10.005) = 10.01 under a decimal policy
*
* Try it: Can this nonnegative half-up snippet be reused unchanged for negative amounts?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/mathematical-language-and-quantitative-reasoning/rounding-precision-tolerance-and-significant-digits/
* Free course: https://courses.thefintechbuilder.com
* Synthetic teaching example, not financial advice or a production library.
*
* Run it: javac Main.java && java Main
*/
public class Main {
static double lesson010() {
long thousandths = 10005; // exact decimal input units; the integer type rules out fractions
if (thousandths < 0)
throw new IllegalArgumentException("Nonnegative integer thousandths only");
long cents = Math.floorDiv(thousandths + 5, 10); // half-up
double result = cents / 100.0; // 10.01
return result;
}
public static void main(String[] args) {
System.out.println(num(lesson010()));
}
// Formats a double the way JSON.stringify does: whole numbers without ".0", null for NaN or infinity.
static String num(double x) {
if (Double.isNaN(x) || Double.isInfinite(x)) return "null";
if (x == Math.rint(x) && Math.abs(x) < 1e15) return String.valueOf((long) x);
return String.valueOf(x);
}
}
No browser runner for Java yet
Read the code here, then run it in your own toolchain or a ready-made cloud workspace.
Expected output
10.01// Fintech Math Bootcamp · Lesson 010 of 120
// Rounding, Precision, Tolerance, and Significant Digits
// Module 01: Mathematical Language and Quantitative Reasoning
//
// Scenario: Rounding a half-cent without guessing binary behavior
// Rule: half-up(10.005) = 10.01 under a decimal policy
//
// Try it: Can this nonnegative half-up snippet be reused unchanged for negative amounts?
//
// Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/mathematical-language-and-quantitative-reasoning/rounding-precision-tolerance-and-significant-digits/
// Free course: https://courses.thefintechbuilder.com
// Synthetic teaching example, not financial advice or a production library.
//
// Run it: go run main.go
package main
import (
"encoding/json"
"errors"
"fmt"
"os"
)
func lesson010() (float64, error) {
var thousandths int64 = 10005 // exact decimal input units; the integer type rules out fractions
if thousandths < 0 {
return 0, errors.New("Nonnegative integer thousandths only")
}
cents := (thousandths + 5) / 10 // half-up: integer division floors nonnegative values
result := float64(cents) / 100 // 10.01
return result, nil
}
func main() {
result, err := lesson010()
if err != nil {
fmt.Fprintln(os.Stderr, "Error:", err)
os.Exit(1)
}
printJSON(result)
}
// printJSON prints a value as JSON indented with two spaces, like JSON.stringify(value, null, 2).
func printJSON(value any) {
out, err := json.MarshalIndent(value, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(out))
}
No browser runner for Go yet
Read the code here, then run it in your own toolchain or a ready-made cloud workspace.
Expected output
10.01/**
* Fintech Math Bootcamp · Lesson 010 of 120
* Rounding, Precision, Tolerance, and Significant Digits
* Module 01: Mathematical Language and Quantitative Reasoning
*
* Scenario: Rounding a half-cent without guessing binary behavior
* Rule: half-up(10.005) = 10.01 under a decimal policy
*
* Try it: Can this nonnegative half-up snippet be reused unchanged for negative amounts?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/mathematical-language-and-quantitative-reasoning/rounding-precision-tolerance-and-significant-digits/
* Free course: https://courses.thefintechbuilder.com
* Synthetic teaching example, not financial advice or a production library.
*
* Run it: g++ -std=c++17 -o main main.cpp && ./main
*/
#include <charconv>
#include <cmath>
#include <cstdint>
#include <iostream>
#include <stdexcept>
#include <string>
// Formats a double the way JSON.stringify does: shortest round-trip form, null for NaN or infinity.
std::string num(double x) {
if (!std::isfinite(x)) return "null";
char buffer[32];
auto [end, error] = std::to_chars(buffer, buffer + sizeof buffer, x);
(void)error;
return std::string(buffer, end);
}
double lesson010() {
const std::int64_t thousandths = 10005; // exact decimal input units; the integer type rules out fractions
if (thousandths < 0)
throw std::invalid_argument("Nonnegative integer thousandths only");
const std::int64_t cents = (thousandths + 5) / 10; // half-up: integer division floors nonnegative values
const double result = static_cast<double>(cents) / 100; // 10.01
return result;
}
int main() {
std::cout << num(lesson010()) << "\n";
}
No browser runner for C++ yet
Read the code here, then run it in your own toolchain or a ready-made cloud workspace.
Expected output
10.01//! Fintech Math Bootcamp · Lesson 010 of 120
//! Rounding, Precision, Tolerance, and Significant Digits
//! Module 01: Mathematical Language and Quantitative Reasoning
//!
//! Scenario: Rounding a half-cent without guessing binary behavior
//! Rule: half-up(10.005) = 10.01 under a decimal policy
//!
//! Try it: Can this nonnegative half-up snippet be reused unchanged for negative amounts?
//!
//! Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/mathematical-language-and-quantitative-reasoning/rounding-precision-tolerance-and-significant-digits/
//! Free course: https://courses.thefintechbuilder.com
//! Synthetic teaching example, not financial advice or a production library.
//!
//! Run it: rustc main.rs && ./main
fn lesson010() -> Result<f64, String> {
let thousandths: i64 = 10005; // exact decimal input units; the integer type rules out fractions
if thousandths < 0 {
return Err("Nonnegative integer thousandths only".to_string());
}
let cents = (thousandths + 5).div_euclid(10); // half-up
let result = cents as f64 / 100.0; // 10.01
Ok(result)
}
fn main() {
match lesson010() {
Ok(result) => println!("{}", num(result)),
Err(message) => {
eprintln!("Error: {}", message);
std::process::exit(1);
}
}
}
/// Formats a number the way JSON.stringify does: shortest round-trip form, null for NaN or infinity.
fn num(x: f64) -> String {
if x.is_finite() {
format!("{}", x)
} else {
"null".to_string()
}
}
No browser runner for Rust yet
Read the code here, then run it in your own toolchain or a ready-made cloud workspace.
Expected output
10.01/**
* Fintech Math Bootcamp · Lesson 010 of 120
* Rounding, Precision, Tolerance, and Significant Digits
* Module 01: Mathematical Language and Quantitative Reasoning
*
* Scenario: Rounding a half-cent without guessing binary behavior
* Rule: half-up(10.005) = 10.01 under a decimal policy
*
* Try it: Can this nonnegative half-up snippet be reused unchanged for negative amounts?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/mathematical-language-and-quantitative-reasoning/rounding-precision-tolerance-and-significant-digits/
* Free course: https://courses.thefintechbuilder.com
* Synthetic teaching example, not financial advice or a production library.
*
* Run it: dotnet run (inside a console project that holds this Program.cs)
*/
using System.Text.Json;
Console.WriteLine(JsonSerializer.Serialize(Lesson010()));
static double Lesson010()
{
const long thousandths = 10005; // exact decimal input units; the integer type rules out fractions
if (thousandths < 0)
throw new ArgumentException("Nonnegative integer thousandths only");
const long cents = (thousandths + 5) / 10; // half-up: integer division floors nonnegative values
var result = cents / 100.0; // 10.01
return result;
}
No browser runner for C# yet
Read the code here, then run it in your own toolchain or a ready-made cloud workspace.
Expected output
10.01Prefer your own machine? Every file is in the course repository · open it in Codespaces.
Lesson notes
The rule
half-up(10.005) = 10.01 under a decimal policy