Jump to content

3 decimal of a float value in VEX or VOP


cgmagic

Recommended Posts

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 by fsimerey
  • Like 1
Link to comment
Share on other sites

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

;)

 

  • Like 1
Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...