Return decimal value (1 Viewer)

Nita

New member
Local time
Today, 23:08
Joined
Feb 14, 2020
Messages
19
Hi,

I have a query with a value in currency, I convert it to number.
I can't get only the decimals, can you help me?
I am using Access 2016
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 19:08
Joined
May 21, 2018
Messages
8,463
Not sure I understand the problem. Would the round function work?
 

Nita

New member
Local time
Today, 23:08
Joined
Feb 14, 2020
Messages
19
Hi,
I want the decimals values.
I need both of the values in separated fields.
The value before the "," I know how to get, the value after is the problem.
For example 5,55 I have one field that returns 5 and want another to return 55.
Thank you
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 19:08
Joined
May 21, 2018
Messages
8,463
x - int(x)
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 19:08
Joined
May 21, 2018
Messages
8,463
Good lesson in floating point arithmitic and things to consider when doing something like this. May not get what you expect without some extra code.
Code:
.

Public Sub Test()
 Dim x As Double
 x = -123.345
 Debug.Print Abs(x) - Int(Abs(x))
End Sub

Public Sub Test2()
 Dim x As Double
 x = -123.345
 Debug.Print CDec(Abs(x)) - CDec(Int(Abs(x)))
End Sub
The first returns
0.344999999999999
The second returns
0.345
 

cheekybuddha

AWF VIP
Local time
Today, 23:08
Joined
Jul 21, 2014
Messages
2,237
You will get the same result as Test2() if you substitute CDec() for CCur()

I'd be tempted to just declare x As Currency:
Code:
Public Sub Test3()
 Dim x As Currency
 x = -123.345
 Debug.Print Abs(x) - Int(Abs(x))
End Sub

In Immediate Window:
Code:
Test3
 0.345
 

Nita

New member
Local time
Today, 23:08
Joined
Feb 14, 2020
Messages
19
Good lesson in floating point arithmitic and things to consider when doing something like this. May not get what you expect without some extra code.
Code:
.

Public Sub Test()
Dim x As Double
x = -123.345
Debug.Print Abs(x) - Int(Abs(x))
End Sub

Public Sub Test2()
Dim x As Double
x = -123.345
Debug.Print CDec(Abs(x)) - CDec(Int(Abs(x)))
End Sub
The first returns
0.344999999999999
The second returns
0.345
In that case, what I need is to return 345.
 

ebs17

Well-known member
Local time
Tomorrow, 00:08
Joined
Feb 7, 2020
Messages
1,882
Currency defines a fixed-point number with exactly four decimal places. How many decimal places are then displayed is irrelevant and only a matter of display and interpretation. You can therefore use integer calculations immediately.
Code:
?123.345 * 10000 mod 10000
 3450

3450 / 10000 = 345 / 1000

Eberhard
 

Users who are viewing this thread

Top Bottom