Decimal Variables Types

 Floating-Point Numbers

 Introduction

A real number is one that displays a decimal part. This means that the number can be made of two sections separated by a symbol that is referred to as the Decimal Separator or Decimal Symbol. This symbol is different by language, country, group of languages, or group of countries. In US English, this symbol is the period as can be verified from the Regional (and Language) Settings of the Control Panel of computers of most regular users:

On both sides of the Decimal Symbol, digits are used to specify the value of the number. The number of digits on the right side of the symbol determines how much precision the number offers.

 Practical Learning: Introducing Decimal Variables
1. Start Microsoft Visual Basic
2. To create a new application, on the main menu, click File -> New -> Project or File -> New Project
3. In the Templates list, click Console Application
4. Set the Name to YugoNationalBank2 and click OK
 Single-Precision

A decimal number is said to have single precision if it can include a decimal part but the precision of the number is not important. In VBasic, such a number has a value that can range from –3.402823e38 and –1.401298e-45 if the number is negative, or 1.401298e-45 and 3.402823e38 if the number is positive.

To declare a variable that can hold small decimal numbers with no concern for precision, use the Single data type. Here is an example:

```Module Module1

Sub Main()
Dim Distance As Single
End Sub

End Module```

Instead of the AS Single expression, you can use the ! symbol as the type character. Here is an example:

```Module Module1

Sub Main()
Dim Distance!
End Sub

End Module```

The ! symbol can also be used in other scenarios in VBasic. Whenever you use it, make sure the word that comes to its right is not the name of a variable.

A Single variable is initialized with 0. After declaring a Single variable, you can declare it with the necessary value. Here is an example:

```Module Module1

Sub Main()
Dim Distance!

Distance! = 195.408
MsgBox(Distance)
End Sub

End Module```

In some cases, the compiler may allocate more memory than necessary for the variable. For example, the above value is more suitable for another type as we will see in the next section. Otherwise, to indicate that the variable must be treated as a decimal number with single-precision, type f or F on the right side of the number. Here is an example:

```Module Module1

Sub Main()
Dim Distance!

Distance! = 195.408f

End Sub

End Module```

To convert a string to a long integer, call CSng(). Enter the value or the expression in the parentheses of CSng(). If the conversion is successful, CSng() produces a Single value.

 Double-Precision

While the Single data type can allow large numbers, it offers less precision. For an even larger number, VBasic provides the Double data type. This is used for a variable that would hold numbers that range from 1.79769313486231e308 to –4.94065645841247e–324 if the number is negative or from 1.79769313486231E308 to 4.94065645841247E–324 if the number is positive.

To declare a variable that can store large decimal numbers with a good level of precision, use the Double keyword.

 In most circumstances, it is preferable to use Double instead of Single when declaring a variable that would hold a decimal number. Although the Double takes more memory spaces (computer memory is not expensive anymore(!)), it provides more precision.

Here is an example of declaring a Double variable:

```Module Module1

Sub Main()
Dim TempFactor As Double
End Sub

End Module```

If you want, you can omit the AS Double expression but use the # symbol instead to declare a Double variable. This can be done as follows:

```Module Module1

Sub Main()
Dim TempFactor#
End Sub

End Module```

A double-precision variable is initialized with a 0 value. After declaring a Double variable, you can initialize it with the needed value. Here is an example:

```Module Module1

Sub Main()
Dim TempFactor#

TempFactor# = 482
MsgBox(TempFactor)
End Sub

End Module```

To indicate that the variable being used must be treated with double precision, enter r or R on its right side. Here is an example:

```Module Module1

Sub Main()
Dim TempFactor#

TempFactor# = 482r
MsgBox(TempFactor)
End Sub

End Module```

To convert a value to a double-precision number, call CDbl() by entering the value or the expression in the parentheses of CDbl(). If CDbl() succeeds, it produces a Double value.

 Practical Learning: Using Double Precision Variables
1. To use the double-precision variables, change the program as follows:

 ```Module Module1 Sub Main() Dim CustomerName Dim AccountNumber Dim InitialDeposit Dim Withdrawals Dim CurrentBalance Dim Result Dim strDeposit, strWithdrawals CustomerName = InputBox("Enter Customer Name:") AccountNumber = InputBox("Enter Account #:") strDeposit = InputBox("Enter Initial Deposit:") strWithdrawals = InputBox("Enter total withdrawals:") InitialDeposit = CDbl(strDeposit) Withdrawals = CDbl(strWithdrawals) CurrentBalance = InitialDeposit - Withdrawals Result = " =-= Yugo National Bank =-=" & vbCrLf & _ "Customer Name: " & CustomerName & vbCrLf & _ "Account Number: " & AccountNumber & vbCrLf & _ "Deposits: \$" & InitialDeposit & vbCrLf & _ "Withdrawals: \$" & Withdrawals & vbCrLf & _ "Current Balance: \$" & CurrentBalance MsgBox(Result) End Sub End Module```
2. Execute the application and provide the requested values. Here is an example: