Home All Groups Group Topic Archive Search About

how 2 approximate 0.1 to be 1

Author
8 Dec 2006 4:48 PM
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

Author
8 Dec 2006 4:59 PM
Charlie Brown
Dim MyNumber as Single = 4.1
Dim MyWholeNumber as Integer = Math.Ceiling(MyNumber)


Hazem wrote:
Show quoteHide quote
> 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
Author
8 Dec 2006 6:16 PM
Hazem
Many many thanks, it worked

You are wounderful

Thanks & Best Regards
Show quoteHide quote
"Charlie Brown" <cbr***@duclaw.com> wrote in message
news:1165597166.323671.200980@79g2000cws.googlegroups.com...
> Dim MyNumber as Single = 4.1
> Dim MyWholeNumber as Integer = Math.Ceiling(MyNumber)
>
>
> Hazem wrote:
>> 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
>
Author
9 Dec 2006 5:44 AM
_AnonCoward
Show quote Hide quote
"Hazem" <hazem.mo***@gmail.com> wrote in message
news:%23CeLxiuGHHA.420@TK2MSFTNGP02.phx.gbl...
: 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