Recent

Author Topic: Strings to floats, and back again.  (Read 8006 times)

pueblonative

  • New member
  • *
  • Posts: 6
Strings to floats, and back again.
« on: June 14, 2006, 09:14:56 pm »
var
d_Total: Extended;
d_SubTotal : Extended;
d_SalesTax: Extended;
s_TotalMessage: string;
begin

d_SubTotal := StrToFloat(edSubtotal.Text);
d_SalesTax := StrToFloat(EdTax.Text);
 if d_SalesTax >= 1.0 then
    d_SalesTax := d_SalesTax / 100;
d_Total := d_SubTotal + (d_SubTotal * d_SalesTax);
s_TotalMessage := 'Total amount is ' + FloatToStr(d_Total);
lblTotal.Text := s_TotalMessage;

end;

Problem is that it displays whole integers, but nothing if it's a float.

CCRDude

  • Hero Member
  • *****
  • Posts: 502
RE: Strings to floats, and back again.
« Reply #1 on: June 15, 2006, 02:55:22 pm »
You could've at least stated platform and version ;) For FPC 2.1.1 (last daily dev source from June 4th) on Win32, I can't reprdouce your problem.

Save this as FloatToStrTest.pas

Code: [Select]
program FloatToStrTest;

uses Crt, SysUtils;

var amount1, amount2, amount3: Extended;

begin
  amount1 := 1234567890.123456789;  // High precision number
  amount2 := 1234567890123456.123;  // High mantissa digits
  amount3 := 1E100;                 // High value number
  WriteLn('FloatToStr test');
  WriteLn('Amount1 = ' + FloatToStr(amount1));
  WriteLn('Amount2 = ' + FloatToStr(amount2));
  WriteLn('Amount3 = ' + FloatToStr(amount3));
end.


And compile using:
Quote
fpc FloatToStrTest.pas


Run it, and at least here it works (Win32):

Quote
FloatToStr test
Amount1 = 1234567890,1234568
Amount2 = 1,23456789012346E15
Amount3 = 1E100

pueblonative

  • New member
  • *
  • Posts: 6
RE: Strings to floats, and back again.
« Reply #2 on: June 15, 2006, 03:34:34 pm »
It works, all right, the problem seems to be more of a string concatination problem on these lines:

s_TotalMessage := 'Total amount is ' + FloatToStr(d_Total);
lblTotal.Text := s_TotalMessage;

lazarus 0.9.0
Linux
fpc 2.0.1

To clarify a bit, when I do this

lblTotal.Text := FloatToStr(d_Total)

it works.
 When I do this:

s_TotalMessage := 'Total amount is ' + FloatToStr(d_Total);
lblTotal.Text := s_TotalMessage;

It will not display floating point numbers.

CCRDude

  • Hero Member
  • *****
  • Posts: 502
RE: Strings to floats, and back again.
« Reply #3 on: June 15, 2006, 05:22:01 pm »
Hmmm I just compiled that with a Lazarus 0.9.17 daily from June 12th (FPC 2.1.1) on Win32 and it worked even with the string thing. I'll try later on a Mac or Linux...

update: Lazarus 0.9.15 on MacOSX did work as well (I added those three controls you mentioned to an empty form, and your code into FormShow; 123.12 for the subtotal, 16 for taxes, resulting in Total amount is 142.8192 displayed in the label).

pueblonative

  • New member
  • *
  • Posts: 6
RE: Strings to floats, and back again.
« Reply #4 on: June 15, 2006, 07:35:55 pm »
BTW, I can post screenshots if that is needed.  I am not making this stuff up.

CCRDude

  • Hero Member
  • *****
  • Posts: 502
RE: Strings to floats, and back again.
« Reply #5 on: June 15, 2006, 08:29:32 pm »
I wasn't thinking about that :D But why don't you use a newer version than 2.0.1/0.9.0? Getting the latest release is usually my first choice when finding bugs ;) (if you're using Debian - imho the debian packages are quite old - you can use alien to convert newer rpms, something I just did on my Ubuntu installation)

eric

  • Sr. Member
  • ****
  • Posts: 268
RE: Strings to floats, and back again.
« Reply #6 on: June 17, 2006, 03:23:06 pm »
As this appears to relate to currency, I suggest that you use TCurrency instead of Extended.

Eric