Basic Processing examples.
Negative numbers to be formatted. Processing documentation states the number to be formatted should be greater than zero.
Poor man's float truncation.
left or right being zero. Processing documentation states these arguments should be greater than zero.
Negative values for left or right. If left is less than zero, it does not change anything left of the decimal. If right is less than zero, it transforms the float into an int.
Note how the way numbers are 'floored' changes if the one's digit is odd or even.
Test written by Matthew Lam
boolean errors=false;
int xcoord=5;
int xoff=10;
int ycoord=35;
int yinc=15;
void setup() {
PFont font;
font = loadFont("Eureka-90.vlw");
textFont(font, 12);
size(900,400);
background(50);
noLoop();
}
void draw() {
output(1,"nf(200, 10)",nf(200, 10),"0000000200");
output(2,"nf(40, 5)",nf(40, 5),"00040");
output(3,"nf(90, 3)",nf(90, 3),"090");
output(4,"nf(200.94, 10, 4)",nf(200.94, 10, 4),"0000000200.9400");
output(5,"nf(40.2, 5, 3)",nf(40.2, 5, 3),"00040.200");
output(6,"nf(9.012, 3, 5)",nf(9.012, 3, 5),"009.01200");
xcoord=200;
ycoord=35;
output(7,"nf(-200, 10)",nf(-200, 10),"-0000000200");
output(8,"nf(-200.94, 10, 4)",nf(-200.94, 10, 4),"-0000000200.9400");
output(9,"nf(-200, 2)",nf(-200, 2),"-200");
output(10,"nf(-200.95, 2, 1)",nf(-200.95, 2, 1),"-200.9");
output(11,"nf(-200, 0)",nf(-200, 2),"-200");
output(12,"nf(-200.94, 2, 0)",nf(-200.94, 2, 0),"-200.94");
xcoord=400;
ycoord=35;
output(13,"nf(-10, -1)",nf(-10, -1),"-10");
output(14,"nf(-10, -3)",nf(-10, -3),"-10");
output(15,"nf(-10.05, -1, -1)",nf(-10.05, -1, -1),"-10");
output(16,"nf(-10.05, -1, -3)",nf(-10.05, -1, -3),"-10");
output(17,"nf(-10.05, 3, -1)",nf(-10.05, 3, -1),"-010");
output(18,"nf(-10.05, 3, -3)",nf(-10.05, 3, -3),"-010");
xcoord=600;
ycoord=35;
output(19,"nf(-10.49, -1, -1)",nf(-10.49, -1, -1),"-10");
output(20,"nf(-10.5, -1, -1)",nf(-10.5, -1, -1),"-10");
output(21,"nf(-11.49, -1, -1)",nf(-11.49, -1, -1),"-11");
output(22,"nf(-11.5, -1, -1)",nf(-11.5, -1, -1),"-12");
output(23,"nf(-12.49, -1, -1)",nf(-12.49, -1, -1),"-12");
output(24,"nf(-12.5, -1, -1)",nf(-12.5, -1, -1),"-12");
output(25,"nf(-13.49, -1, -1)",nf(-13.49, -1, -1),"-13");
output(26,"nf(-13.5, -1, -1)",nf(-13.5, -1, -1),"-14");
xcoord=800;
ycoord=35;
output(27,"nf(-14.49, -1, -1)",nf(-14.49, -1, -1),"-14");
output(28,"nf(-14.5, -1, -1)",nf(-14.5, -1, -1),"-14");
output(28,"nf(-15.49, -1, -1)",nf(-15.49, -1, -1),"-15");
output(29,"nf(-15.5, -1, -1)",nf(-15.5, -1, -1),"-16");
output(30,"nf(-16.49, -1, -1)",nf(-16.49, -1, -1),"-16");
output(31,"nf(-16.5, -1, -1)",nf(-16.5, -1, -1),"-16");
output(31,"nf(-17.49, -1, -1)",nf(-17.49, -1, -1),"-17");
output(32,"nf(-17.5, -1, -1)",nf(-17.5, -1, -1),"-18");
String errMsg="";
if (errors) {
fill(255,0,0);
errMsg="RED tests FAILED.";
} else {
fill(0,255,0);
errMsg="All tests passed.";
}
text(errMsg,5,20);
}
void output(int ind,String txt,String results,String expected) {
fill(255,255,255);
text("["+ind+"] "+txt,xcoord,ycoord+=yinc);
if (results.equals(expected)) {
fill(0,255,0);
} else {
fill(255,0,0);
errors=true;
}
text(results,xcoord+xoff,ycoord+=yinc);
ycoord+=yinc;
}