Numbers are one of the most important data types in programming languages. It is used everywhere like a calculator, managing bank transactions, processing sensor data, or controlling robots.
In the Mojo programming language, numbers allow us to store, calculate, and manipulate numerical data easily.
What Are Numbers in Mojo?
Numbers are data values that represent quantities like 5 to 100 or decimals (3.14 or -0.5). We can use numbers in Mojo for the following terms:
- Perform mathematical calculations such as addition, subtraction, multiplication, and division.
- It can store counts or measurements.
- Work with scientific or financial data.
Numbers are a data type that Mojo can store in variables.
Types of Numbers in Mojo
Mojo supports several number types for different works. Such as:
a) Integer (Int)
- It contains whole numbers, but no decimals.
- It can be positive, negative, or zero.
Example:
var age: Int = 25
var temperature: Int = -5
var items: Int = 0
print(age, temperature, items)
Output:
25 -5 0
b) Floating Point (Float64 or Float32)
This Data type contains numbers with decimal points. It’s mostly used when precision is important, like in scientific measurements, currency.
For example:
var price: Float64 = 19.99
var pi_value: Float64 = 3.14159
print(price, pi_value)
Output:
19.99 3.14159
c) Boolean as Numbers
It is a different type that represents numbers in Boolean values, such as True and False.
- True = 1
- False = 0
For example:
var is_sunny: Bool = True
print(Int(is_sunny)) # Output: 1
We have provided all the Operators in this course. Click here and learn it.
How To Convert Number Type?
We can change a number from one type to another using type casting method.
For example:
var num_int: Int = 5
var num_float: Float64 = Float64(num_int)
print(num_float) # Output: 5.0
Real-World Example: Grocery Bill Calculator
Let’s create a real-life example by combining integers and floats.
fn calculate_bill(apples: Int, bananas: Int, apple_price: Float64, banana_price: Float64):
var total_cost = (apples * apple_price) + (bananas * banana_price)
print(f"Total Bill: ₹{total_cost}")
# Example use
calculate_bill(4, 6, 12.5, 8.75)
Output:
Total Bill: ₹112.5
This program similar to how real supermarkets calculate bills with mixing quantities (Int) and prices (Float).
Number Functions and Methods in Mojo
Mojo doesn’t have built-in math libraries like Python, but you can still:
- Use operators for calculations.
- Create custom functions for tasks like finding averages, percentages, etc.
Example – Percentage Calculator:
fn percentage(marks: Float64, total: Float64) -> Float64:
return (marks / total) * 100
print(percentage(450, 500)) # Output: 90.0
This Mojo code defines a percentage function that takes two numbers: marks obtained and total marks, and calculates the percentage by dividing marks by total and multiplying by 100.
When we call percentage(450, 500), it works out (450 / 500) * 100, which equals 90.0, and then prints it.
Exercise For Practice
BMI (Body Mass Index) Calculator
Write a Mojo program that takes a person’s weight in kilograms and height in meters, then calculates their Body Mass Index using the formula:
BMI = weight / (height * height)
The program should:
- Print the calculated Body Mass Index.
- Use if, elif, else to print:
- “Underweight” if BMI is less than 18.5
- “Normal” if BMI is between 18.5 and 24.9
- “Overweight” if BMI is between 25 and 29.9
- “Obese” if BMI is 30 or above
Example Output:
BMI: 22.04
Category: Normal
project code for understanding purposes only:
fn main():
# Step 1: Input weight and height (fixed values for this exercise)
var weight: Float64 = 68.0 # in kilograms
var height: Float64 = 1.75 # in meters
# Step 2: Calculate BMI using the formula
var bmi: Float64 = weight / (height * height)
# Step 3: Print the BMI value (rounded to 2 decimal places)
print("BMI:", round(bmi, 2))
# Step 4: Decide category based on BMI ranges
if bmi < 18.5:
print("Category: Underweight")
elif bmi >= 18.5 and bmi <= 24.9:
print("Category: Normal")
elif bmi >= 25.0 and bmi <= 29.9:
print("Category: Overweight")
else:
print("Category: Obese")
Write this code by yourself and try to understand what happens in this code and how it works.
Also Learn this Topics:
- What Is Mojo Programming Language In 2025?
- What Are Variables In Mojo?
- What Are Data Types In Mojo?
- What Is Type Casting In Mojo?
- What Are The Constants In Mojo?
- What Are the Operators In Mojo?
- What are the for and while loops in Mojo?
- What are the Functions in Mojo?
- What is the Lambda in Mojo?
- What are the Strings in Mojo?