preface

This section follows the second section: numeric types, string types

Video presentation address: Portal

numeric

There are two types of values in Dart: ints and doubles

The int type

Int Is an integer of any size.

The concept of integers in mathematics is as follows:

An integer is a collection of positive, zero, and negative integers.

Look at an example:

Null is printed if it is declared as assignment, and we saw why in the first lecture. All declared unassigned variables in Dart are null.

Type double

The Dart Double is a 64-bit floating point number specified in the IEEE 754 standard.

Because Dart also uses IEEE 754 64-bit floating-point types, as does javascript, some well-known floating-point accuracy issues still exist in Dart, as shown below.

0.1 + 0.2 != 0.3

23.6-23! = 0.6

It’s the same scenario in javascript, because we follow the same floating-point rules.

You can pay attention to this point, if you want to know the specific principle, I will write a separate article on this kind of situation in two days.

What we need to pay attention to is that we can pay attention to the precision in the usual development.

Properties and Methods

Here we will focus on some properties and methods of int variables.

hashcode

Returns a hash code for a numeric value. Look at an example:

Output:

isFinite

True if the number is finite;

What is the infinite case? Dart has a situation like this

isInfinite

Whether the number is positive or negative infinity. Return false if neither;

isNaN

True if the number is a double non-numeric value; Otherwise, yes.

isNegative

If the number is negative, this property returns the Boolean value true.

isEven & isOdd

Whether it’s even, whether it’s odd

abs()

Returns the absolute value of a number.

ceil()

Returns the smallest integer not less than the number

floor()

Returns the largest integer not greater than the current number

compareTo(x)

It returns an integer representing the relationship between two numbers.

  • 0 – if the values are equal.
  • 1 – If the current number object is greater than the specified value.
  • -1 – If the current numeric object is smaller than the specified numeric value.

round()

This method returns the value rounded to the nearest integer. It can be understood as rounding

toDouble()

Integer to double – precision floating – point type.

toInt()

Double – precision floating – point type to integer. In cropping mode, without rounding

toString()

This method returns a string representation of a numeric value

There are also some properties and methods that are not very commonly used. I won’t go into details here, but you can understand the above examples with javascript in mind.

string

There are several representations of strings in Dart.

  • Single-line strings are written in single or double quotation marks
  • Multiline strings are written in triple quotes
  • Single quotation marks contain double quotation marks
  • Double quotation marks contain single quotation marks
  • Triple quotation marks include double quotation marks and single quotation marks

Output:

String splicing

String concatenation using+.

Adjacent splice

$splicing

Use $if concatenation is a variable

If concatenation is an expression, use ${}

Properties and Methods

Index operator

A String of type String accesses the desired String representation of the code through the index operator

String cutting

Using subString to cut the string, returns the cut index range content

isEmpty

Whether this string is empty.

isNotEmpty

Whether this string is not empty

length

Get the length of the string

trim()

Removes whitespace around the string and returns

compareTo()

Compares the relationship between two strings

Relational comparison table:

  • 0 – When the strings are equal.
  • 1 – When the first string is greater than the second string
  • -1 – When the first string is smaller than the second string

This order is the same as the order of the code points at the first position where the two strings differ. If one string is a prefix to another, the shorter string is sorted before the longer string. Strings are equivalent in sorting if they have exactly the same content. Sorting does not check for Unicode equivalence. Comparisons are case sensitive.

replaceAll()

Replaces all substrings that match the specified pattern with the given value.

Support for regular expressions

replaceRange()

ReplaceRange The first argument is the start subscript, the second argument is the end subscript, and the third argument is the string to be replaced. The replacement string contains the start index but does not contain the end index. The number of the start index must be greater than or equal to 0, and the value of the end index must be less than or equal to the length of the string. Otherwise, an error will be reported.

split()

Replaces all substrings that match the specified pattern with the given value.

END

The third section explains that numeric type, string type, the difference between these two knowledge points and javascript is more, we need to pay attention to the use of the next. I intend to write two branch articles, on hashCode and on IEEE floating point type principles.