how 2 approximate 0.1 to be 1

  • Thread starter Thread starter Hazem
  • Start date Start date
H

Hazem

Hi, I want to make a calculation & need to appriximate

any fraction after the point to be added as genuine

like 4.1 = 5

what function or syntax can I use to accomplish that in VB.net ??

Thanks

Hazem
 
Dim MyNumber as Single = 4.1
Dim MyWholeNumber as Integer = Math.Ceiling(MyNumber)
 
: Hi, I want to make a calculation & need to appriximate
:
: any fraction after the point to be added as genuine
:
: like 4.1 = 5
:
: what function or syntax can I use to accomplish that in VB.net ??
:
: Thanks
:
: Hazem


Two approaches come to mind. Both are illustrated below:

---------------------------------------------------------
Option Strict

Imports System

Public Module [module]

PUblic Sub Main

Print_Combined_Digits_1(4.0)
Print_Combined_Digits_1(4.1)
Print_Combined_Digits_1(4.2)
Print_Combined_Digits_1(4.3)
Print_Combined_Digits_1(4.4)
Print_Combined_Digits_1(4.5)
Print_Combined_Digits_1(4.6)
Print_Combined_Digits_1(4.7)
Print_Combined_Digits_1(4.8)
Print_Combined_Digits_1(4.9)
Print_Combined_Digits_1(5.0)

Console.WriteLine()

Print_Combined_Digits_2(4.0)
Print_Combined_Digits_2(4.1)
Print_Combined_Digits_2(4.2)
Print_Combined_Digits_2(4.3)
Print_Combined_Digits_2(4.4)
Print_Combined_Digits_2(4.5)
Print_Combined_Digits_2(4.6)
Print_Combined_Digits_2(4.7)
Print_Combined_Digits_2(4.8)
Print_Combined_Digits_2(4.9)
Print_Combined_Digits_2(5.0)

End Sub

Private Sub Print_Combined_Digits_1(Number As Single)

Dim Scratch As String = FormatNumber(Number, 1)

Dim Major As Integer = CInt(Int(Number))
Dim Minor As Integer = CInt(Int(Number * 10)) Mod (Major * 10)
Dim Combined As Integer = Major + Minor

Console.WriteLIne(Scratch & ": " & Major & " + " & Minor & _
" = " & Combined)

End Sub

Private Sub Print_Combined_Digits_2(Number As Single)

Dim Scratch As String = FormatNumber(Number, 1)
Dim Digits() As String = Split(Scratch, ".")

Dim Major As Integer = CInt(Digits(0))
Dim MInor As Integer = CInt(Digits(1))
Dim Combined As Integer = Major + Minor

Console.WriteLIne(Scratch & ": " & Major & " + " & Minor & _
" = " & Combined)

End Sub

End Module
---------------------------------------------------------


This generates the following output:

4.0: 4 + 0 = 4
4.1: 4 + 1 = 5
4.2: 4 + 2 = 6
4.3: 4 + 3 = 7
4.4: 4 + 4 = 8
4.5: 4 + 5 = 9
4.6: 4 + 6 = 10
4.7: 4 + 7 = 11
4.8: 4 + 8 = 12
4.9: 4 + 9 = 13
5.0: 5 + 0 = 5

4.0: 4 + 0 = 4
4.1: 4 + 1 = 5
4.2: 4 + 2 = 6
4.3: 4 + 3 = 7
4.4: 4 + 4 = 8
4.5: 4 + 5 = 9
4.6: 4 + 6 = 10
4.7: 4 + 7 = 11
4.8: 4 + 8 = 12
4.9: 4 + 9 = 13
5.0: 5 + 0 = 5

Ralf
 
Back
Top