Parsing and Converting Numbers
Turn strings into numbers and back.
Parsing and Converting Numbers is a free Dart Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Text Is Not a Number
User input and files arrive as strings. The text "42" cannot do math until you parse it into a real number first. 🔄
Parse to an int
Call int.parse to turn a digit string into an int. The result is a true number you can add, compare, or store.
void main() {
int n = int.parse('42');
print(n + 1); // 43
}Parse to a double
For decimal text, use double.parse. It reads the dot and gives you a floating-point value ready for fractional math.
void main() {
double d = double.parse('3.14');
print(d); // 3.14
}Bad Input Throws
If the text is not a valid number, parse throws a FormatException and stops your program. Unchecked input is risky.
void main() {
int.parse('hello'); // FormatException
}Safer With tryParse
Use int.tryParse when input might be messy. It returns null instead of throwing, so you can handle the failure calmly.
void main() {
print(int.tryParse('oops')); // null
}Default on Failure
Combine tryParse with the ?? operator to supply a fallback. If parsing fails, you quietly use a sensible default value.
void main() {
int n = int.tryParse('x') ?? 0;
print(n); // 0
}Number to String
Go the other way with toString. It turns any number into text you can join, save, or show to your user.
void main() {
print(42.toString()); // '42'
}Control Decimal Places
Use toStringAsFixed to format a double with a set number of decimals. Perfect for prices and tidy reports.
void main() {
print(3.14159.toStringAsFixed(2)); // '3.14'
}Parsing With a Radix
int.parse accepts a radix to read other bases. Pass 16 to interpret a hex string, like turning "ff" into 255.
void main() {
print(int.parse('ff', radix: 16)); // 255
}num Parses Either Kind
When the text could be int or double, num.parse figures it out. It returns the most fitting numeric type for you.
void main() {
print(num.parse('7')); // 7
print(num.parse('7.5')); // 7.5
}Trim Before You Parse
Stray spaces can trip parsing. Call trim first so " 42 " becomes a clean "42" the parser accepts without complaint.
void main() {
print(int.parse(' 42 '.trim())); // 42
}Quick Check
Which call avoids a crash on invalid input?
Recap
Use parse for trusted text and tryParse with ?? for risky input. Convert back with toString and toStringAsFixed. ✅
Frequently asked questions
Is the “Parsing and Converting Numbers” lesson free?
Yes — the full text of “Parsing and Converting Numbers” is free to read here on the web, and the Dart Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Dart Academy course, upgrade to CoddyKit PRO.
What will I learn in “Parsing and Converting Numbers”?
Turn strings into numbers and back. You practise Dart Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Dart Academy?
No prior experience is required. Dart Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Parsing and Converting Numbers” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Dart Academy lesson?
Yes. Every Dart Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Arithmetic and Operator Precedence
- int vs double and Integer Division
- Parsing and Converting Numbers
- The dart:math Toolkit