> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/NormandoRamirezDelgado/6C-Febrero-2026/llms.txt
> Use this file to discover all available pages before exploring further.

# Type Conversions

> Learn how to convert between different data types in Dart

## 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.

<Note>
  Dart performs **explicit type conversion** - you must explicitly convert types using built-in methods. This prevents accidental type-related bugs.
</Note>

## Common Type Conversions

<CardGroup cols={3}>
  <Card title="String → int" icon="arrow-right">
    Parse text to integer
  </Card>

  <Card title="int → String" icon="arrow-right">
    Convert number to text
  </Card>

  <Card title="int → double" icon="arrow-right">
    Convert integer to decimal
  </Card>

  <Card title="double → int" icon="arrow-right">
    Convert decimal to integer
  </Card>

  <Card title="String → double" icon="arrow-right">
    Parse text to decimal
  </Card>

  <Card title="double → String" icon="arrow-right">
    Convert decimal to text
  </Card>
</CardGroup>

## String to int Conversion

Use `int.parse()` to convert a String containing a number into an integer:

```dart theme={null}
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)
}
```

<Warning>
  `int.parse()` will throw an error if the string doesn't contain a valid integer. For example, `int.parse('hello')` will crash your program.
</Warning>

### Safe Parsing with tryParse

For safer parsing that won't crash your program, use `int.tryParse()`:

```dart theme={null}
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:

```dart theme={null}
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

```dart theme={null}
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):

```dart theme={null}
void main() {
  int numeroUno = 123;
  
  // Convert int to double
  double dobleUno = numeroUno.toDouble();
  
  print(numeroUno);   // 123
  print(dobleUno);    // 123.0
}
```

<Tip>
  Converting to double is useful when you need decimal precision in calculations, even if your starting value is a whole number.
</Tip>

## double to int Conversion

Use `.toInt()` to convert a double to an integer. **This truncates the decimal part** (doesn't round):

```dart theme={null}
void main() {
  double dobleDos = 1234523456243563567.86348623487623465;
  
  // Convert double to int (truncates decimal)
  int entero = dobleDos.toInt();
  
  print(dobleDos);  // 1.2345234562435636e+18
  print(entero);    // 1234523456243563520
}
```

<Warning>
  `.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`
</Warning>

### Rounding Doubles to Integers

If you want to round instead of truncate, use these methods:

```dart theme={null}
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:

```dart theme={null}
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:

```dart theme={null}
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

```dart theme={null}
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

```dart theme={null}
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

```dart theme={null}
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

| From   | To          | Method           | Example                         |
| ------ | ----------- | ---------------- | ------------------------------- |
| String | int         | `int.parse()`    | `int.parse('42')` → `42`        |
| String | int (safe)  | `int.tryParse()` | `int.tryParse('42')` → `42`     |
| String | double      | `double.parse()` | `double.parse('3.14')` → `3.14` |
| int    | String      | `.toString()`    | `42.toString()` → `'42'`        |
| int    | double      | `.toDouble()`    | `42.toDouble()` → `42.0`        |
| double | String      | `.toString()`    | `3.14.toString()` → `'3.14'`    |
| double | int         | `.toInt()`       | `3.14.toInt()` → `3`            |
| double | int (round) | `.round()`       | `3.14.round()` → `3`            |
| double | int (floor) | `.floor()`       | `3.9.floor()` → `3`             |
| double | int (ceil)  | `.ceil()`        | `3.1.ceil()` → `4`              |

## Error Handling

<Warning>
  Always validate string input before parsing to avoid runtime errors:
</Warning>

```dart theme={null}
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.
