Dart: Converting Numbers To Strings

Converting numbers to strings means changing a number like 123 or 4.56 into text that looks like “123” or “4.56”. This helps when you want to show numbers in messages, print them, or join them with words.

You might want to convert numbers to strings when building apps, writing messages, or formatting data for display. It makes it easy to mix numbers and words together.

Using .toString() Method

You can convert any number to a string by using the .toString() method. This method changes a number into text so you can combine it with other strings.

Here is an example where we have a number age set to 25. We convert it to a string with .toString() and then print it with a message:

void main() {

  int age = 25;
  print('My age is ' + age.toString());

}

This code prints the message “My age is” followed by the number 25 converted into text.

You can use .toString() with any number type, like int or double. For example:

void main() {

  double price = 9.99;
  print('Price: \$' + price.toString());

}

This code prints the word “Price:” followed by the number 9.99 converted into text.

String Interpolation

String interpolation is an easy way to include numbers or other values directly inside a string. Instead of using .toString() and adding strings together, you use $ followed by the variable name inside the string.

Here’s an example where we include a number inside a message:

void main() {

  int score = 42;
  print('Your score is $score');

}

This code prints a message that shows the value of score inside the text.

If you want to include an expression or something more complex, you put it inside ${} like this:

void main() {

  int apples = 3;
  int oranges = 5;

  print('Total fruits: ${apples + oranges}');

}

This code calculates the sum of apples and oranges and shows it inside the string. String interpolation makes your code simple and easy to read.

Formatting Numbers with .toStringAsFixed()

You can convert a number to a string and control how many decimal places to show using .toStringAsFixed(). This is useful for prices, scores, or measurements where you want a fixed number of decimals.

Here’s an example that shows a price with two decimal places:

void main() {

  double price = 9.999999;
  print('Price: \$' + price.toStringAsFixed(2));

}

This code turns the number price into a string with exactly two decimals, then adds it to the message. The \$ shows a dollar sign in the output. This method helps keep numbers looking neat and consistent.

Using .toStringAsPrecision() for Significant Digits

You can convert a number to a string that shows a specific number of significant digits using .toStringAsPrecision(). This helps when you want to control how many important digits appear, no matter where the decimal point is.

Here’s an example that shows a measurement with 3 significant digits:

void main() {

  double measurement = 123.4567;
  print('Measurement: ' + measurement.toStringAsPrecision(3));

}

This code takes the number measurement and converts it to a string with three important digits. It’s useful when you want your numbers clear but not too long.

Using .toStringAsExponential() for Scientific Notation

If you want to show a number in scientific (exponential) notation as a string, use .toStringAsExponential(). This is helpful for very large or very small numbers.

You can also choose how many digits to show after the decimal point.

Here’s an example showing a number in exponential form with 2 decimal places:

void main() {

  double bigNumber = 1234567.89;
  print('Exponential: ' + bigNumber.toStringAsExponential(2));

}

This code converts bigNumber to a string in scientific notation, showing 2 digits after the decimal point. It makes big numbers easier to read in a compact form.

Convert Number to String with Radix

You can convert a number to a string in different bases using .toRadixString(). For example, base 2 is binary, and base 16 is hexadecimal.

This helps when you want to show numbers in different number systems.

void main() {

  int number = 42;

  print('Binary: ' + number.toRadixString(2)); // binary  
  print('Hex: ' + number.toRadixString(16));   // hex  

}

In this code, the number 42 is shown as a binary string and a hex string. This lets you easily convert numbers into other counting systems and display them as text.

Fun Example: Number to String Puzzle

This example shows how to mix numbers and strings in a playful way using .toString(). It converts the number 7 to a string and joins it with other words to make a fun sentence.

void main() {

  int day = 7;
  print('Today is day ' + day.toString() + ' of the week!');

}

The code prints a message saying which day of the week it is, by turning the number into text and combining it with other words.

Conclusion

In Dart, you can convert numbers to strings in many simple ways. Use .toString() for basic conversion, and .toStringAsFixed() to fix decimal places. For controlling significant digits, use .toStringAsPrecision(). To show numbers in scientific notation, use .toStringAsExponential(). If you want to convert numbers to different bases like binary or hex, use .toRadixString().

String interpolation is also a clean and easy way to combine numbers and strings without extra conversions.