Try:
the_nr := trunc(the_nr * 100 + 0.5) / 100.0;
Thanks.
That works like my original function though, in that, on my Windows Vista PC (32 bit windows) running Lazarus v0.9.30, using your function with the_nr set to 14.321 then converting to string using floattostr gives "14.32" [correct]
Though on my Win 7, 64 bit PC, Lazarus v0.9.30.4, FPC 2.6.0 (like with my original function) the result your function gives after using floattostr is "14.3199996948242"
whereas some of the other methods give a 2 dp (eg 14.32) result after using floattostr on both versions of Lazarus/FPC/on both my PCs, so I think the other methods are more accurate, at least on this PC/Lazarus/FPC version.
Though if you break your version down into more statements,
eg.
the_nr:=14.321;
the_nr:=(the_nr*100+0.5);
the_nr:=trunc(the_nr);
the_nr:=the_nr/100.0; It gives the more precise answer (after using floattostr), even on my Win 7 64 bit machine, ie. "14.32" not "14.319999694824".
I would have expected that the compiler
should have generated the same code for both versions (the 1 line version of the code, and the 3 line version of it), though that can't be the case, and it seems that on this version of Lazarus/FPC the compiler generates less accurate code (ie. one that makes a less accurate calculation result) in the first version of the code (using your code all in one statement) than the compiler does if the same calculation is written in 3 seperate statements.
Also, I don't think it's just a case of "floattostr" not giving an accurate result (nor only because of inaccuracies with floating point numbers in general), as I've tried it with my own function that converts an extended to a string (and using an extended instead of a real for the_nr - even though I think trunc uses a normal real not extended), and the version of your function using 3 statements instead of 1 also gives a lot more accurate result using that.