13.3 Values, types and variables
- Approved by
- Panaversity ·
In everyday life. The characters 4-8-2-5-0-0 can be an amount, a postal code or a part number. You add amounts. You never add postal codes. What you may do with the characters depends on what kind of thing they are, not on how they look.
What you will learn. What the kind of data allows, why text from a file stays text, and why money is kept in whole cents.
Why it matters. A number read from a file is only text until the code turns it into a number, so check that the code does this before it adds anything.
A value is one piece of data. Its type is the kind of value it is, and the type decides what you can do with it. A variable is a name you give a value so you can use it again.
| Kind of value | TypeScript | Python | Brightline example |
|---|---|---|---|
| Text | string | str | "INV-8301", "Net 30" |
| Whole number | number | int | 482500 cents |
| Decimal number | number | float | Never used for money |
| True or false | boolean | bool | Is the invoice open? |
| Nothing | null or undefined | None | A record with no due date at all |
What is a type for?
A type label is an instruction. When you write one on a variable, such as cents: number, you tell the next reader, the type checker and the agent what is allowed. An agent asked to "write this function with these types" follows the labels. Choosing the types is part of the first 10 percent.
Why whole cents?
The ordinary decimal numbers in both languages use a format called binary floating point. It cannot hold most decimal fractions exactly, just as you cannot write one third exactly as a decimal: 0.3333 has to stop somewhere. So 1.10 + 2.20 is not exactly 3.30. This book keeps every amount in whole cents, and turns cents into dollars only when it prints them, with whole-number math. Real financial systems use whole small units like cents. Or they use a decimal type with a fixed number of decimal places and stated rounding rules, such as Python's Decimal. Never let an agent store money in an ordinary floating-point number. Whole cents have a limit too. A TypeScript number holds whole numbers exactly only up to about $90 trillion in cents, far above any Brightline total.
Why is an amount from a file still text?
A program cannot tell an amount from a postal code by looking, either. Everything it reads from a CSV file arrives as text, including the amounts. "482500" is six characters, not a number, until your code converts it. Until then it compares as text, character by character. This is a common source of wrong answers in agent-written scripts that read files, and it is what went wrong in Renee's list.
At work. The lab download has a script an agent wrote to total the open invoices. It reads each amount from the file and adds it to a running total that starts at 0. The TypeScript version prints a 75-digit number. The Python version stops with an error. It is the same idea and the same mistake, with two very different results.
In TypeScript. Predict all four lines before you look.
const fromFile = "482500";
const other = "61875";
console.log(fromFile + other);
console.log(Number(fromFile) + Number(other));
console.log(0 + fromFile);
console.log(1.10 + 2.20, 110 + 220);48250061875
544375
0482500
3.3000000000000003 330+ on two strings joins them together. Number(...) converts text to a number, and it ignores blank space at the ends. Text that is not a number, such as "4,825.00", gives NaN, which means "not a number". 0 + "482500" joins too, because TypeScript turns the 0 into text. The last line shows why money is kept in cents.
In Python.
from_file = "482500"
other = "61875"
print(from_file + other)
print(int(from_file) + int(other))
print(1.10 + 2.20, 110 + 220)
print(0 + from_file)48250061875
544375
3.3000000000000003 330
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for +: 'int' and 'str'Where the languages differ.
| TypeScript | Python | |
|---|---|---|
| Number plus text | Joins them into text, and keeps running | Stops with a TypeError |
| Numbers | One number type for whole and decimal | int for whole, float for decimal |
| Convert text to a number | Number("482500") | int("482500") |
The TypeScript behavior is the dangerous one, because the program keeps running with a wrong value.
The operating rule: convert text from a file into the type it stands for as soon as the code reads it, keep money in whole cents, and label the types so a checker can check the code against them.

Figure 13.3. Text or number. The characters are the same. The type decides what plus means.
Practice it. Practice: values and types, in your own tool, ten minutes.
Check yourself
Question 1 / 8
0 answered
fromFile is "482500" and other is "61875". What does fromFile + other print?
- Owner
- Panaversity