OneBite.Dev - Coding blog in a bite size

Declare A Float Number In Dart

Code snippet for how to Declare A Float Number In Dart with sample and detail explanation

Dart programming language is used extensively in software development. This article will focus on showing you how to declare a float number in Dart language.

Code snippet to Declare A Float Number In Dart

In Dart programming language, declaring a float number is a straightforward task. Here is a sample demonstration:

void main() {
  double num1 = 5.23;
  print('The value of num1 is $num1');
}

Code Explanation for Declare A Float Number In Dart

In the Dart language, double is used to declare a floating point number. In this tutorial, we break down each step in the code snippet:

  1. void main() {...} - This is the main function where the execution of the code begins. Dart’s foundation library provides a top-level function, main(), which is the entry point of the application. Without this function, the application cannot execute.

  2. double num1 = 5.23; - In this line, num1 is a variable name and double is a data type which specifies that num1 is a floating point number. 5.23 is the value assigned to the variable num1. In Dart, you don’t require to specify the data type explicitly; Dart will automatically infer that num1 is of double type.

  3. print('The value of num1 is $num1'); - This command is used to print the value of the variable num1. Here $num1 is a template literal used to reference the value of the variable num1. When this line of code executes, it outputs: “The value of num1 is 5.23”.

And that completes our tutorial on how to declare a float number in Dart language. Keep practicing and have fun coding!

dart