Skip to main content

Introduction

Type conversion (also called type casting) is the process of converting a value from one data type to another. In Dart, you’ll often need to convert between types, such as turning a String into an int, or an int into a double.
Dart performs explicit type conversion - you must explicitly convert types using built-in methods. This prevents accidental type-related bugs.

Common Type Conversions

String → int

Parse text to integer

int → String

Convert number to text

int → double

Convert integer to decimal

double → int

Convert decimal to integer

String → double

Parse text to decimal

double → String

Convert decimal to text

String to int Conversion

Use int.parse() to convert a String containing a number into an integer:
void main() {
  String variable = '50';
  
  // Convert String to int
  int numero = int.parse(variable);
  
  print(numero);        // 50 (as integer)
  print(numero + 10);   // 60 (can now do math)
}
int.parse() will throw an error if the string doesn’t contain a valid integer. For example, int.parse('hello') will crash your program.

Safe Parsing with tryParse

For safer parsing that won’t crash your program, use int.tryParse():
void main() {
  String validNumber = '42';
  String invalidNumber = 'hello';
  
  int? result1 = int.tryParse(validNumber);    // 42
  int? result2 = int.tryParse(invalidNumber);  // null
  
  print(result1);  // 42
  print(result2);  // null
}

int to String Conversion

Use .toString() to convert any number to a String:
void main() {
  int numero = 50;
  
  // Convert int to String
  String valor = numero.toString();
  
  print(valor);           // '50' (as String)
  print(valor + ' km');   // '50 km' (string concatenation)
}

Working with Large Numbers

void main() {
  int numeroUno = 123;
  int numeroDos = 6524200889711234;
  
  print(numeroDos);                    // 6524200889711234
  print(numeroDos.toString());         // '6524200889711234'
}

int to double Conversion

Use .toDouble() to convert an integer to a double (decimal number):
void main() {
  int numeroUno = 123;
  
  // Convert int to double
  double dobleUno = numeroUno.toDouble();
  
  print(numeroUno);   // 123
  print(dobleUno);    // 123.0
}
Converting to double is useful when you need decimal precision in calculations, even if your starting value is a whole number.

double to int Conversion

Use .toInt() to convert a double to an integer. This truncates the decimal part (doesn’t round):
void main() {
  double dobleDos = 1234523456243563567.86348623487623465;
  
  // Convert double to int (truncates decimal)
  int entero = dobleDos.toInt();
  
  print(dobleDos);  // 1.2345234562435636e+18
  print(entero);    // 1234523456243563520
}
.toInt() truncates (cuts off) the decimal part, it doesn’t round. For example:
  • 3.9.toInt() returns 3 (not 4)
  • 3.1.toInt() returns 3
  • -3.9.toInt() returns -3

Rounding Doubles to Integers

If you want to round instead of truncate, use these methods:
void main() {
  double number = 3.7;
  
  print(number.toInt());       // 3 (truncate)
  print(number.round());       // 4 (round to nearest)
  print(number.floor());       // 3 (round down)
  print(number.ceil());        // 4 (round up)
}

String to double Conversion

Use double.parse() to convert a String to a double:
void main() {
  String precio = '19.99';
  
  // Convert String to double
  double valor = double.parse(precio);
  
  print(valor);          // 19.99
  print(valor * 2);      // 39.98
}

Complete Conversion Example

Here’s a comprehensive example showing all the common conversions:
void main() {
  // String to int
  String variable = '50';
  int numero = int.parse(variable);
  print('String to int: $numero');
  
  // int to String
  String valor = numero.toString();
  print('int to String: $valor');
  
  // int to double
  int numeroUno = 123;
  double dobleUno = numeroUno.toDouble();
  print('int to double: $dobleUno');
  
  // double to int
  double dobleDos = 1234.56789;
  int entero = dobleDos.toInt();
  print('double to int: $entero');
  
  // String to double
  String textoDecimal = '99.99';
  double precio = double.parse(textoDecimal);
  print('String to double: $precio');
  
  // double to String
  String precioTexto = precio.toString();
  print('double to String: $precioTexto');
}

Practical Use Cases

User Input Conversion

void main() {
  // User inputs are always strings
  String userInput = '25';  // Simulating user input
  
  // Convert to int for calculations
  int edad = int.parse(userInput);
  
  if (edad >= 18) {
    print('You are an adult');
  } else {
    print('You are a minor');
  }
}

Calculations with Mixed Types

void main() {
  int quantity = 5;
  double pricePerUnit = 12.50;
  
  // Convert int to double for precise calculation
  double total = quantity.toDouble() * pricePerUnit;
  
  print('Total: \$${total}');  // Total: $62.5
}

Formatting Numbers for Display

void main() {
  int score = 95;
  double average = 87.5;
  
  // Convert to strings for display
  String message = 'Score: ${score.toString()}, Average: ${average.toString()}';
  print(message);
}

Conversion Methods Summary

FromToMethodExample
Stringintint.parse()int.parse('42')42
Stringint (safe)int.tryParse()int.tryParse('42')42
Stringdoubledouble.parse()double.parse('3.14')3.14
intString.toString()42.toString()'42'
intdouble.toDouble()42.toDouble()42.0
doubleString.toString()3.14.toString()'3.14'
doubleint.toInt()3.14.toInt()3
doubleint (round).round()3.14.round()3
doubleint (floor).floor()3.9.floor()3
doubleint (ceil).ceil()3.1.ceil()4

Error Handling

Always validate string input before parsing to avoid runtime errors:
void main() {
  String input = 'not a number';
  
  // ❌ BAD: Will crash if input is invalid
  // int number = int.parse(input);  // FormatException!
  
  // ✅ GOOD: Safe parsing
  int? number = int.tryParse(input);
  if (number != null) {
    print('Valid number: $number');
  } else {
    print('Invalid input');
  }
}

Key Takeaways

  • Use int.parse() or int.tryParse() to convert String to int
  • Use double.parse() or double.tryParse() to convert String to double
  • Use .toString() to convert any number to String
  • Use .toDouble() to convert int to double
  • Use .toInt() to convert double to int (truncates decimals)
  • Use .round(), .floor(), or .ceil() for rounding doubles
  • Always handle potential parsing errors with tryParse() methods

Next Steps

Now that you understand type conversions, you’re ready to explore operators and how to perform calculations and comparisons in Dart.

Build docs developers (and LLMs) love