Forum > General

StrToFloat - formatted

(1/11) > >>

J-G:
I may be trying to accomplish the impossible but being aware of  FormatFloat & FloatToStrF I can't find a means by which the opposite may be accomplished.

Given a String '2.5625'  I want to return the number 2.5625  NOT  2.562500023841 . . . . . .

I am aware of 'SameValue' using a small value for the tolerance but have not been able to use that effectively.

This question arose due to an incorrect comparison failure which I still cannot fathom. I created a new Project to demonstrate the issue but was surprised when I couldn't reproduce the same result that I had seen in my main project. This involves comparing two values to determine if the first is an integer.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Good := (T1*Ratio) = Int(T1*Ratio);In the main project, with T1 (byte) being 20 and Ratio (Single) being 2.54999952 (which ought to be 2.55) >:(  'Good' is [False]
but I KNOW that 20x2.55 = 51 (integer)
whereas in the test project:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Good := (G1*Ratio) = Int(G1*Ratio); with G1 (byte) = 20 and Ratio exactly as before, Good returns [True] !  %)
A .zip file of the test project is attached.

I've found a way to force the correct result (for 'Good') in the main project by adding two variables declared as 'single' and comparing those :

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---      A := T1*Ratio;      B := Int(A);      Good := A = B;
... but the logic behind this defeats me. :(







Thaddy:

--- Code: ---var s:string;
begin
   writestr(s, 2.5625:4);
   writeln(s);
end.
--- End code ---

Bart:

--- Quote from: J-G on June 25, 2022, 12:53:11 pm ---Given a String '2.5625'  I want to return the number 2.5625  NOT  2.562500023841 . . . . . .

--- End quote ---

StrToFloat('2.5625') gives me 2.562500000000000000 (when the result value is a Double).
FormatFloat & FloatToStrF have nothing to do with that (given the quote above).

Bart

Thaddy:
I made a slight mistake:
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$mode objfpc}var s:string;begin    writestr(s,2.5625:4:4); // note the format punctuation   writeln(s);end.Is correct. It is not the writeln, but the writestr that is actually formatting. It can be both, but writestr is usable in GUI appications. The writeln is just to verify its output. The above code is correct.

https://wiki.freepascal.org/WriteStr
https://www.freepascal.org/docs-html/rtl/system/write.html
https://www.freepascal.org/docs-html/rtl/system/writeln.html
https://www.freepascal.org/docs-html/rtl/system/writestr.html

All fully documented.
Once you know that, it is much easier to use than FloatToStr and family.

J-G:

--- Quote from: Thaddy on June 25, 2022, 02:01:47 pm ---
--- Code: ---var s:string;
begin
   writestr(s, 2.5625:4);
   writeln(s);
end.
--- End code ---

--- End quote ---
Thanks for the input Thaddy but I have no problem in adjusting the  DISPLAY  of a float (or any number really)  it's making the VALUE  accurate to the real number of decimal places which is my difficulty. 

I've now seen your edit/correction  which adds another dimension which I hadn't considered (I did know that the second argument was needed) - the subtle difference between 'writestr' & 'writeln' - I seldom write 'console' programs and have never used the former.

I now see - thanks to the link you provided - that there is also a ReadStr which I assume is the equivalent of StrToFloat -- where WriteStr is = to FloatToStr  -- I'll now have to see whether there is any benefit in using these methods to convert Strings to Values.

@Bart  may have put his finger on the underlying issue though - I tend to use the smallest 'container' for any variable so seldom need 'Double' or beyond - maybe I need to take a broader approach :)

Navigation

[0] Message Index

[#] Next page

Go to full version