处理 - frameRate 小数

Processing - frameRate decimals

我已经找了一段时间没有解决方案,可能是因为我正在使用处理。

我在 window 的角上创建了一个 "fps" 标记,但它有很多小数点。我如何缩短它以仅显示总共两个或三个数字?

相关代码:

text("FPS: "+frameRate,100,100);

提前致谢。

*我使用 2.2 而不是 3,所以 nf() thinky 不可行

使用带有 3 个参数的 nf() 函数,它在 Processing 2.2 中可用:

void draw(){
    background(0);
    String fps = nf(frameRate, 2, 2);
    text("FPS: "+fps, 0, 50);
}

只接受2个参数的nf()函数是在Processing 3中添加的,但是在Processing 2.2中你仍然可以使用接受3个参数的nf()函数:要格式化的数字,数字小数点左边的位数,小数点右边的位数)。

您也可以自己格式化:

void draw(){
    background(0);
    String fps = str(frameRate).substring(0, 4);
    text("FPS: "+fps, 0, 50);
}

或者,如果您实际上并不关心小数点,只需将其转换为 int 即可:

void draw(){
    background(0);
    int fps = (int)frameRate;
    text("FPS: "+fps, 0, 50);
}