Android Question TRUNC function for rounding doubles

bluedude

Well-Known Member
Licensed User
Longtime User
Hi,

I need to round my doubles like:

1.488 = 1.40

It should be working like the Excel trunc function which does that. Any suggestions?
 

DonManfred

Expert
Licensed User
Longtime User
1.488 to 1.4 is not round(x,1)... I think you should use Numberformat in this case.
 
Upvote 0

derez

Expert
Licensed User
Longtime User
B4X:
Dim n As Double = 1.488
Dim m As Double = Floor(n*10)/10
This returns 1.4 , I don't know how to force the 0 to appear after the 4.
 
Upvote 0

ac9ts

Active Member
Licensed User
Longtime User
If you are keeping 1.4 as a double, the math routines will treat it as 1.4 no matter how many 0's are at the end of if.

If you are printing/displaying 1.4 as a string, try just a simple string append:

B4X:
Dim n As Double = 1.488
Dim m As Double = Floor(n*10)/10
Dim s As String = m & "0"

BTW, In Excel 2007, TRUNC(1.488,1) returns 1.4
 
Upvote 0
Top