Something (could be money) for nothing?

enonod

Well-Known Member
Licensed User
Longtime User
I am finding it a little long winded to separate a decimal from its integer and must have missed something. This gives me more than I started with, Great for a bank or the tax man's computer.
I can either cast to an int variable or Floor() with odd results.

I actually want to obtain the 0.6 or whatever accurately. I have achieved it as shown at the bottom but wow.

B4X:
Dim w,v as Float
Dim z, y as int

w = 12.6

z = Floor(w)    
Log(z)  =  12

'or I could cast to int with the same result
y = w   =  12

v = w - z
Log(v)  =    0.6000003814697266

    :)   Log(z + v)  =  12.6000003814697266  :)

I could  Floor( (v - w) * 10 ) /10   =  0.6
 
Last edited:

stevel05

Expert
Licensed User
Longtime User
Have you tried a regex.split?
 
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
No, and if it is not like a sherbet split then I will try it.
I thought regex was for strings and so it would never have occurred to me to look at that. Things get weirder and weirder, perhaps I am too old fashioned and should stick to a calculator.

Thank you very much for that.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
OK, I did just play with it, the "." is a special character and needs to be escaped so your statement will be:

B4X:
Dim Result() As String
Result=Regex.Split("\.",w)

For i = 0 to Result.Length-1
   Log(Result(i))
Next

in B4A the float gets converted automatically to a string when needed.

And no, it's not quite like a Sherbert split!
 
Last edited:
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
Thank you again. I am now spoilt for choice, and I don't know why the obvious did not occur before, especially as I have been using Mod all day...

[EDIT] Dim w as Double

12.65 Mod 1

[Edit1] The regex method gave you 65 so I would then have to divide by 100

[EDIT2] It occurs to me that the final division used would vary depending on how many digits followed the point, which makes matters worse.
 
Last edited:
Upvote 0

AndyDroid2012

Member
Licensed User
Longtime User
Dim x,w As Int
Dim y As String
y = "12.6543210987"
x = Floor(y)
w = Round(y)
Log(x)
Log(w)
Log(y - x) ----> 0.6543210987000005


for Y = "12.65" -------------> 0.6500000000000004
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Mod is probably the simplest way to go, of there was a lot of calculations and it became time sensitive, you could test for the fastest.

With the regex you could concatenate "0." & Result(1).

via Tapatalk
 
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
It has proved to be an interesting subject. I am always grateful for input and in this instance I have found that the judicial use of casting to the right variable types coupled with Mod 1 seems to work flawlessly.
Thank you both for your input.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…