Get As It Is 2 digits value after decimal without rounding it (1 Viewer)

Ashfaque

Student
Local time
Tomorrow, 03:45
Joined
Sep 6, 2004
Messages
894
I have encountered a problem while calculating 2 digits.

I don’t know if this question has already been asked and answered on this forum. But I need to get it solved.

Let us say I have a value 452.65

I need to get only last digits as it is in a variable without rounding as whole.


Textbox1=452.65

Textbox2=452

Textbox1=65

I tried many ways but it is either rounding or displaying 0.65


Can anyone help me please….
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 17:15
Joined
Feb 28, 2001
Messages
27,167
Textbox 2 should be easy - [TextBox2] = FIX( [Textbox1] )

Textbox 1 should ALSO be relatively easy - [TextBox1] = FIX( 100.0 * ( [Textbox1] - [TextBox2] ) )

Just remember to computer TextBox2 first.

If these numbers can be negative, it might be trickier.
 

Ashfaque

Student
Local time
Tomorrow, 03:45
Joined
Sep 6, 2004
Messages
894
Correction please...

extbox1=452.65

Textbox2=452

Textbox3=65

I need to get Textbox3 value which will be 65 and not 0.65

I tried with FIX function but showing 0 value
 

Ashfaque

Student
Local time
Tomorrow, 03:45
Joined
Sep 6, 2004
Messages
894
I am really stuck up due to this prblm.

SImply I have value in text box called "AmtAsPerActivitySheet" on the form. The value let us say 2556425.87 or whatever with 2 decimals at end.

I need to read this 87 in another textbox without making it round figure. Mean I nead 87 only..neither 9 or 0.87.

Please help....
 

Minty

AWF VIP
Local time
Today, 23:15
Joined
Jul 26, 2013
Messages
10,371
Textbox2 = INT(Me.txtBox1) 'Int does not round

TextBox3 = INT((Textbox1 - Int(Textbox1)) * 100)

This will fix the Textbox3 to 2 places, I assume that is what you want.
 

Gasman

Enthusiastic Amateur
Local time
Today, 23:15
Joined
Sep 21, 2011
Messages
14,260
Int() would have been my way as well.
I am hoping that you can so simple maths?
 

Ashfaque

Student
Local time
Tomorrow, 03:45
Joined
Sep 6, 2004
Messages
894
Thanks Gasman,

I tried that way but 0 is appearing. So I tried other way as follows:

Dim A As String
A = Format((Y), "0000000000000.00")
Dim B As Single
B = Val(A) - Int(Val(A))

Dim A1 As Double
A1 = Val(Right(B, 1))
D1 = Val(Left(B, 1))

in Above when the input value is 0.91 ( I supplied any value let us say 452.91 that displays value for A is 0000000000452.91 so accordingly B is showing 0.91)
Now when I put condition
If (A1 > 0 And D1 = 0) Then

Here is A1 is showing Right value of 0.91 which is 1
But D1 is showing 0 whereas it has to show 9 (as left value in 0.91)

This is my main problem
 

Minty

AWF VIP
Local time
Today, 23:15
Joined
Jul 26, 2013
Messages
10,371
Use the immediate window to check?
If you can it produces some interesting results :

? Int(452.65)
452
? (452.65 - Int(452.65))
0.649999999999977

Does your starting number always have just 2 decimal places?
And is it a number or a string? You are formatting a string by the look of your second code.
 

Ashfaque

Student
Local time
Tomorrow, 03:45
Joined
Sep 6, 2004
Messages
894
Use the immediate window to check?
If you can it produces some interesting results :

? Int(452.65)
452
? (452.65 - Int(452.65))
0.649999999999977

Does your starting number always have just 2 decimal places?
And is it a number or a string? You are formatting a string by the look of your second code.
Yes always there shall be 2 decimal places.
If 90 then D1 should show 0 as second digit
If 91 then D1 should show 1 as seoond digit
if 92 then D1 should show 2 n likewize

I tried in immediate window it show as follows:
?int(452.91)
452
?452-Int(452.91)
0

Why I dont know but it should show 1 not ZERO
 

Gasman

Enthusiastic Amateur
Local time
Today, 23:15
Joined
Sep 21, 2011
Messages
14,260
I would expect zero if you do this?

?452-Int(452.91)

However as Minty has pointed out, you have to be careful.?

What is the reason for not using round() ?
 

Ashfaque

Student
Local time
Tomorrow, 03:45
Joined
Sep 6, 2004
Messages
894
Gasman,

Counting 2 digits after decimal is necessary in my process to convert numbers to words. In English 0-9 it is simple but reading fashion in Arabic langugar is different. Example if the amount is 125 so in English we read One Hundred Twenty Five. But in Arabic it will be read One Hundred Fifty Tow.
Means last digit will be considered first and second last will be on secondary place.

We have sorted out the code for all the figures appearing before decimal but after decimal if 10, 20, 30 ...90 is appearing as decimal value, we must read both figure in form of A1 & B1. So we can compare and send control to other code lines where the decimal's result will be TEN, TWENTY, THIREY....(in Arabic)

we have no issue in digits appearing from 11-19, 21-29, 31-39 ........91-99

So this was the reason I need to read these 2 digits after decimal.

Thanks for your support.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 23:15
Joined
Sep 12, 2006
Messages
15,651
Let us say I have a value 452.65

There might be a slight issue if the decimal portion is not exactly 2 dps.
It might actually be, say, 452.647. It may be 452.645 or 452.655. Now 452.645 would normally round to 2dps as 452.64, and 452.655 as 452.66, (bankers rounding) so you do need to be sure whether 452.65 is a rounded number or a discrete value exactly represented in binary. It might not make a difference to the final result, but conceivably, it might. If you expect any number like 452.645 to be rounded UP to 452.65, and not rounded to the nearest even number - 452.64, then you will need different code.

Assuming the value with 2ps is the one you need to work with, then first you need to take 100 times the number to get rid of the decimals, which multiplies 452.65 to 45265 and then round it to 45265. That's why if your number of 452.65 is already rounded or truncated from a different number, you need to take some care.

Anyway, now you can get the 65 by just modding the number by 100. The mod function gives you a remainder. 45265 mod 100 = 65

So you might end up with
Code:
decimalvalue = round(myvar*100,2) mod 100

Now, you just need to check that the above formula does what you expect for values of your variable.
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 23:15
Joined
Sep 21, 2011
Messages
14,260
Gasman,

Counting 2 digits after decimal is necessary in my process to convert numbers to words. In English 0-9 it is simple but reading fashion in Arabic langugar is different. Example if the amount is 125 so in English we read One Hundred Twenty Five. But in Arabic it will be read One Hundred Fifty Tow.
Means last digit will be considered first and second last will be on secondary place.

We have sorted out the code for all the figures appearing before decimal but after decimal if 10, 20, 30 ...90 is appearing as decimal value, we must read both figure in form of A1 & B1. So we can compare and send control to other code lines where the decimal's result will be TEN, TWENTY, THIREY....(in Arabic)

we have no issue in digits appearing from 11-19, 21-29, 31-39 ........91-99

So this was the reason I need to read these 2 digits after decimal.

Thanks for your support.
So you can still round, then swap the digits? :unsure:
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 06:15
Joined
May 7, 2009
Messages
19,230
on the New Textbox, set the Controlsource to:

=Val(Replace$(CStr(CDec([AmtAsPerActivitySheet]) - CDec(Fix([AmtAsPerActivitySheet]))), ".", ""))
 

Ashfaque

Student
Local time
Tomorrow, 03:45
Joined
Sep 6, 2004
Messages
894
There might be a slight issue if the decimal portion is not exactly 2 dps.
It might actually be, say, 452.647. It may be 452.645 or 452.655. Now 452.645 would normally round to 2dps as 452.64, and 452.655 as 452.66, (bankers rounding) so you do need to be sure whether 452.65 is a rounded number or a discrete value exactly represented in binary. It might not make a difference to the final result, but conceivably, it might. If you expect any number like 452.645 to be rounded UP to 452.65, and not rounded to the nearest even number - 452.64, then you will need different code.

Assuming the value with 2ps is the one you need to work with, then first you need to take 100 times the number to get rid of the decimals, which multiplies 452.65 to 45265 and then round it to 45265. That's why if your number of 452.65 is already rounded or truncated from a different number, you need to take some care.

Anyway, now you can get the 65 by just modding the number by 100. The mod function gives you a remainder. 45265 mod 100 = 65

So you might end up with
Code:
decimalvalue = round(myvar*100,2) mod 100

Now, you just need to check that the above formula does what you expect for values of your variable.
I think I done with your this code line......Let me check all possible entries from 01-99 after decimal and let you know soon.....Thanks a lot.
 

Users who are viewing this thread

Top Bottom