Module 1 · Mathematical Language and Quantitative Reasoning Lesson 3 of 120
Ratios, Proportions, Rates, and Percentages
Comparing approval rates across merchants.
Transcript
25 sentences · select one to jump thereCheck your understanding
A 2% fee on 100 is how much?
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 003 of 120
* Ratios, Proportions, Rates, and Percentages
* Module 01: Mathematical Language and Quantitative Reasoning
*
* Scenario: Comparing approval rates across merchants
* Rule: rate = approved / attempted
*
* Try it: A 2% fee on 100 is how much?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/mathematical-language-and-quantitative-reasoning/ratios-proportions-rates-and-percentages/
* Free course: https://courses.thefintechbuilder.com
* Synthetic teaching example, not financial advice or a production library.
*/
export function lesson003() {
const approved = 700;
const attempted = 1000;
if (attempted <= 0) throw new Error("No denominator");
const rate = approved / attempted;
const result = {rate, percent: 100 * rate};
return result;
}
export const checkedResult = {"rate":0.7,"percent":70};
// Run this file directly: npx tsx lessons/01-mathematical-language-and-quantitative-reasoning/003-ratios-proportions-rates-and-percentages.ts
if (process.argv[1] && import.meta.url.endsWith(process.argv[1].replace(/\\/g, "/").split("/").pop()!)) {
console.log(JSON.stringify(lesson003(), null, 2));
}
Your output
Press Run to execute the code in your browser.
Expected output
{
"rate": 0.7,
"percent": 70
}"""
Fintech Math Bootcamp · Lesson 003 of 120
Ratios, Proportions, Rates, and Percentages
Module 01: Mathematical Language and Quantitative Reasoning
Scenario: Comparing approval rates across merchants
Rule: rate = approved / attempted
Try it: A 2% fee on 100 is how much?
Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/mathematical-language-and-quantitative-reasoning/ratios-proportions-rates-and-percentages/
Free course: https://courses.thefintechbuilder.com
Synthetic teaching example, not financial advice or a production library.
Run it: python main.py
"""
import json
def lesson003() -> dict:
approved = 700
attempted = 1000
if attempted <= 0:
raise ValueError("No denominator")
rate = approved / attempted
result = {"rate": rate, "percent": 100 * rate}
return result
if __name__ == "__main__":
print(json.dumps(lesson003(), indent=2))
Your output
Press Run to execute the code in your browser.
Expected output
{
"rate": 0.7,
"percent": 70
}/**
* Fintech Math Bootcamp · Lesson 003 of 120
* Ratios, Proportions, Rates, and Percentages
* Module 01: Mathematical Language and Quantitative Reasoning
*
* Scenario: Comparing approval rates across merchants
* Rule: rate = approved / attempted
*
* Try it: A 2% fee on 100 is how much?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/mathematical-language-and-quantitative-reasoning/ratios-proportions-rates-and-percentages/
* 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 final class ApprovalRate {
final double rate;
final double percent;
ApprovalRate(double rate, double percent) {
this.rate = rate;
this.percent = percent;
}
}
static ApprovalRate lesson003() {
double approved = 700;
double attempted = 1000;
if (attempted <= 0) throw new IllegalArgumentException("No denominator");
double rate = approved / attempted;
ApprovalRate result = new ApprovalRate(rate, 100 * rate);
return result;
}
public static void main(String[] args) {
ApprovalRate result = lesson003();
System.out.println(object("rate", num(result.rate), "percent", num(result.percent)));
}
// 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);
}
static String quote(String text) {
return "\"" + text.replace("\\", "\\\\").replace("\"", "\\\"") + "\"";
}
// Indents an already formatted JSON value by one level.
static String nested(String json) {
return json.replace("\n", "\n ");
}
static String object(String... keysAndValues) {
if (keysAndValues.length == 0) return "{}";
StringBuilder out = new StringBuilder("{");
for (int i = 0; i < keysAndValues.length; i += 2) {
out.append(i == 0 ? "\n " : ",\n ")
.append(quote(keysAndValues[i])).append(": ").append(nested(keysAndValues[i + 1]));
}
return out.append("\n}").toString();
}
}
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
{
"rate": 0.7,
"percent": 70
}// Fintech Math Bootcamp · Lesson 003 of 120
// Ratios, Proportions, Rates, and Percentages
// Module 01: Mathematical Language and Quantitative Reasoning
//
// Scenario: Comparing approval rates across merchants
// Rule: rate = approved / attempted
//
// Try it: A 2% fee on 100 is how much?
//
// Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/mathematical-language-and-quantitative-reasoning/ratios-proportions-rates-and-percentages/
// 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"
)
type ApprovalRate struct {
Rate float64 `json:"rate"`
Percent float64 `json:"percent"`
}
func lesson003() (ApprovalRate, error) {
approved := 700.0
attempted := 1000.0
if attempted <= 0 {
return ApprovalRate{}, errors.New("No denominator")
}
rate := approved / attempted
result := ApprovalRate{Rate: rate, Percent: 100 * rate}
return result, nil
}
func main() {
result, err := lesson003()
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
{
"rate": 0.7,
"percent": 70
}/**
* Fintech Math Bootcamp · Lesson 003 of 120
* Ratios, Proportions, Rates, and Percentages
* Module 01: Mathematical Language and Quantitative Reasoning
*
* Scenario: Comparing approval rates across merchants
* Rule: rate = approved / attempted
*
* Try it: A 2% fee on 100 is how much?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/mathematical-language-and-quantitative-reasoning/ratios-proportions-rates-and-percentages/
* 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 <iostream>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
// 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);
}
std::string quote(const std::string& text) {
std::string out = "\"";
for (char c : text) {
if (c == '"' || c == '\\') out += '\\';
out += c;
}
return out + "\"";
}
// Indents an already formatted JSON value by one level.
std::string nested(const std::string& json) {
std::string out;
for (char c : json) {
out += c;
if (c == '\n') out += " ";
}
return out;
}
std::string object(const std::vector<std::pair<std::string, std::string>>& fields) {
if (fields.empty()) return "{}";
std::string out = "{";
for (std::size_t i = 0; i < fields.size(); ++i) {
out += i == 0 ? "\n " : ",\n ";
out += quote(fields[i].first) + ": " + nested(fields[i].second);
}
return out + "\n}";
}
struct ApprovalRate {
double rate;
double percent;
};
ApprovalRate lesson003() {
const double approved = 700;
const double attempted = 1000;
if (attempted <= 0) throw std::invalid_argument("No denominator");
const double rate = approved / attempted;
const ApprovalRate result{rate, 100 * rate};
return result;
}
int main() {
const ApprovalRate result = lesson003();
std::cout << object({{"rate", num(result.rate)}, {"percent", num(result.percent)}}) << "\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
{
"rate": 0.7,
"percent": 70
}//! Fintech Math Bootcamp · Lesson 003 of 120
//! Ratios, Proportions, Rates, and Percentages
//! Module 01: Mathematical Language and Quantitative Reasoning
//!
//! Scenario: Comparing approval rates across merchants
//! Rule: rate = approved / attempted
//!
//! Try it: A 2% fee on 100 is how much?
//!
//! Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/mathematical-language-and-quantitative-reasoning/ratios-proportions-rates-and-percentages/
//! Free course: https://courses.thefintechbuilder.com
//! Synthetic teaching example, not financial advice or a production library.
//!
//! Run it: rustc main.rs && ./main
struct ApprovalRate {
rate: f64,
percent: f64,
}
fn lesson003() -> Result<ApprovalRate, String> {
let approved = 700.0;
let attempted = 1000.0;
if attempted <= 0.0 {
return Err("No denominator".to_string());
}
let rate = approved / attempted;
let result = ApprovalRate { rate, percent: 100.0 * rate };
Ok(result)
}
fn main() {
match lesson003() {
Ok(result) => println!(
"{}",
object(&[("rate", num(result.rate)), ("percent", num(result.percent))])
),
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()
}
}
fn quote(text: &str) -> String {
format!("\"{}\"", text.replace('\\', "\\\\").replace('"', "\\\""))
}
/// Indents an already formatted JSON value by one level.
fn nested(json: &str) -> String {
json.replace('\n', "\n ")
}
fn object(fields: &[(&str, String)]) -> String {
if fields.is_empty() {
return "{}".to_string();
}
let lines: Vec<String> = fields
.iter()
.map(|(key, value)| format!(" {}: {}", quote(key), nested(value)))
.collect();
format!("{{\n{}\n}}", lines.join(",\n"))
}
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
{
"rate": 0.7,
"percent": 70
}/**
* Fintech Math Bootcamp · Lesson 003 of 120
* Ratios, Proportions, Rates, and Percentages
* Module 01: Mathematical Language and Quantitative Reasoning
*
* Scenario: Comparing approval rates across merchants
* Rule: rate = approved / attempted
*
* Try it: A 2% fee on 100 is how much?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/mathematical-language-and-quantitative-reasoning/ratios-proportions-rates-and-percentages/
* 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;
var jsonOptions = new JsonSerializerOptions { WriteIndented = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
Console.WriteLine(JsonSerializer.Serialize(Lesson003(), jsonOptions));
static ApprovalRate Lesson003()
{
const double approved = 700;
const double attempted = 1000;
if (attempted <= 0) throw new InvalidOperationException("No denominator");
var rate = approved / attempted;
var result = new ApprovalRate(rate, 100 * rate);
return result;
}
record ApprovalRate(double Rate, double Percent);
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
{
"rate": 0.7,
"percent": 70
}Prefer your own machine? Every file is in the course repository · open it in Codespaces.
Lesson notes
The rule
rate = approved / attempted