Resulting Decimal Value Wrong in range 0.01 to 0.06 (1 Viewer)

Ashfaque

Student
Local time
Today, 20:51
Joined
Sep 6, 2004
Messages
894
Code:
Dim A, Z As String
Dim MyDecimal, LessThanOne, SevenDigits As String

        A = Format((Y), "0000000000000.00")       
        Dim B As Single
        B = Val(A) - Int(Val(A))
      
        Dim A1, D1, DecimalValue As Integer
        LessThanOne = Int(Y)
          
        DecimalValue = Round(B * 100, 2) Mod 100
        
         A1 = Val(Right(DecimalValue, 1))
         B1 = Val(Left(DecimalValue, 1))

When input value for 1 to max 99,999,999.99, the result is producing CORRECT.

But when input value a between 999,999,999.01 and 999,999,999.06 it giving decimal value wrong. After .07 the result is coming correct.

Please help
 

Minty

AWF VIP
Local time
Today, 15:21
Joined
Jul 26, 2013
Messages
10,355
You aren't declaring your variables correctly

Dim MyDecimal, LessThanOne, SevenDigits As String

Will result in MyDecimal, LessThanOne being declared as Variants.
So start by declaring all your variables correctly.

Secondly, what are the results you are expecting? You are telling us it is incorrect but what would the right result be.
We need Starting Data and then Expected results.

And finally what is format ((Y) , "0000000000000.00") achieving?
 

Ashfaque

Student
Local time
Today, 20:51
Joined
Sep 6, 2004
Messages
894
Minty,

This is to count fractional part of a value that I need to be in variable A1 & B1.

Means if the value is entered 2542.06 then fractional part 06 should be displayed at A1=6 and B1=0
So I can get thier respective ARABIC WORDS from further codes.

In my supplied value there shall be always max 2 decimal this decimal value I need to read.

When I am supplying whole number 999,999,999.01 it is displaying A1=1 but B1=9 or 6 or 5 where as it should read 0

format ((Y) , "0000000000000.00") is assumed to be 12 + 2 decimal value that will be entered.
 

Minty

AWF VIP
Local time
Today, 15:21
Joined
Jul 26, 2013
Messages
10,355
Okay, I thought it might be related to the same goal as the other thread.

I personally think you are making this really over complicated.

Assuming your value never has more than two decimal places, or if it does you aren't worried about them this can be done pretty easily.

Code:
Function fnDecimalsReversed(ByRef cYourValue As Currency) As String

    Dim sInVal As String
    Dim sDec As String
    Dim sLeftDec As String
    Dim sRightDec As String
   
    sInVal = CStr(Format(cYourValue, "#.00"))

    sDec = Right(sInVal, 2)
    sLeftDec = Left(sDec, 1)
    sRightDec = Right(sDec, 1)
   
    fnDecimalsReversed = sRightDec & sLeftDec
   

End Function

Sub testDecs()
    Dim cValue As Currency
    Dim sAns As String
   
    For cValue = 9999999999.01 To 9999999999.99 Step 0.01
        sAns = fnDecimalsReversed(cValue)
        Debug.Print cValue; sAns
    Next cValue
   
       
End Sub

Sample output from the immediate window;
Code:
testdecs
9999999999.01 10
9999999999.02 20
9999999999.03 30
9999999999.04 40
9999999999.05 50
9999999999.06 60
9999999999.07 70
9999999999.08 80
9999999999.09 90
9999999999.1 01
9999999999.11 11
9999999999.12 21
9999999999.13 31
9999999999.14 41
9999999999.15 51
9999999999.16 61
9999999999.17 71
9999999999.18 81
9999999999.19 91
9999999999.2 02
9999999999.21 12
9999999999.22 22
9999999999.23 32
9999999999.24 42
9999999999.25 52
9999999999.26 62
9999999999.27 72
9999999999.28 82
9999999999.29 92
9999999999.3 03
9999999999.31 13
 

Minty

AWF VIP
Local time
Today, 15:21
Joined
Jul 26, 2013
Messages
10,355
Glad it helped.

When you get stuck, rather than attacking the whole problem in one go, try and break it down into really small obvious steps.
The function above could almost certainly be delivered in one or two more complicated VBA lines, but when you separate it out into individual small chunks the process becomes a lot clearer to see.
 

Users who are viewing this thread

Top Bottom