Nf() as text() parameter?

I am developing code using Processing 3.5.4 on PC, exporting to Linux, copying exported files from armv6hf to Rpi Buster on Rpi 2 hardware. Since nf() returns a string, or at least I thought it did, I’m curious why the following code is not displaying 35678

float x = 35678.234;
text( nf(x,0,0), 200, 200 );

The program displays 35678.234. The only way I can display a whole number is by casting the float into an int variable.

I can’t get it to display anything but the whole number using nf(number, 0, 0). Regardless of storing it in a string or not.

This worked for me:
text(nfs(x,0,-1), 200, 200 );

float f1 = 999.999;
String S1 = "this";
int x1 = 0;

void setup()
{
  size(800,400);
  background(0);
}

void draw()
{
    text(nf(f1,0,0),400,200);

    S1 = nf(f1,0,0);
    text(S1,400,220);

    x1 = (int)f1;
    text(x1,400,240);
}

I get the same result.

Apparently, nf() also rounds the number depending on number of significant digits shown.

float f1 = 999.789;

String S1;
int x1 = 0;

void setup()
{
  size(200,200);
}

void draw()
{
  background(0);

  text(nf(f1,0,0),50,50);

  S1 = nf(f1,0,2);
  text(S1,50,70);

  S1 = nf(f1,0,-1);
  text(S1,50,90);


  x1 = (int)f1;
  text(x1,50,120);
}

nf problem

Thanks for confirming. Looks like a bug to me based upon behavior contrary to documentation. Just wondering if documentation is out of date.

Yes, it does seem that the case of nf(number, 0, 0) and nf(number, 0, -1) is undocumented.

I checked documentation page more closely and it states “The values for the digits and right parameters should always be positive integers.” So I’m guessing that means this function isn’t supposed to be used to create a whole number the way I’ve been attempting it. I’m assuming that means they don’t support negative numbers either, even though it works.

1 Like

There’s also the round() function for that btw.