cgmagic Posted December 9, 2016 Share Posted December 9, 2016 How do I get 3 decimal of a float value in VEX or VOP ? There is a rint() but this only convert to integer. Quote Link to comment Share on other sites More sharing options...
fsimerey Posted December 9, 2016 Share Posted December 9, 2016 f@y = rint(@P.y * 1000) / 1000; If you want 3 decimal for a string, with zero on decimal, you must extract fraction and decimal, then concatenate those in string. Quote Link to comment Share on other sites More sharing options...
fsimerey Posted December 9, 2016 Share Posted December 9, 2016 (edited) float a = 179.9996; string trunc = itoa(int(trunc(a))); int ifrac = int(rint(frac(a)*1000)); string frac = "000"; if (ifrac == 1000) { trunc = itoa(int(trunc(a)) +1); } else { frac = sprintf("%03d", ifrac); } s@result = trunc + "." + frac; Here the solution for string. Edited December 9, 2016 by fsimerey 1 Quote Link to comment Share on other sites More sharing options...
f1480187 Posted December 9, 2016 Share Posted December 9, 2016 string whole = itoa((int) some_float); string fract = sprintf("%f", frac(some_float)); string result = whole + fract[1:4]; Here is what I used a couple of times. 2 Quote Link to comment Share on other sites More sharing options...
fsimerey Posted December 9, 2016 Share Posted December 9, 2016 18 minutes ago, f1480187 said: string whole = itoa((int) some_float); string fract = sprintf("%f", frac(some_float)); string result = whole + fract[1:4]; Here is what I used a couple of times. Like your solution but the round result is not accurate. Try it with 3 decimals and 0.9996 Your solution result with 0.999 when the accurate must give 1.000 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.