Recent

Author Topic: Number with decimal  (Read 4641 times)

asdf

  • Sr. Member
  • ****
  • Posts: 310
Number with decimal
« on: February 05, 2011, 07:55:46 pm »
str1:='100';
str2:='6';

If I would like to divide str1 by str2 and the result is 16.67, not 16.666666666666666
What should I do ?

str1:='32.59';
str2:='40.17';
How can I multiply str1 by str2 and the result has 2 decimal points ?

Lazarus 1.2.4 / Win 32 / THAILAND

Blaazen

  • Hero Member
  • *****
  • Posts: 3239
  • POKE 54296,15
    • Eye-Candy Controls
Re: Number with decimal
« Reply #1 on: February 05, 2011, 09:25:09 pm »
If you need only display result with 2 decimal places:

var f1, f2: LongInt;  //or Single, or Double ....
begin
f1:=strtoint(str1);  //or strtofloat
f2:=strtoint(str2);  //or strtofloat

floattostrF(f1/f2, ffFixed, 3, 2);    //important is the last place (2)
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

asdf

  • Sr. Member
  • ****
  • Posts: 310
Re: Number with decimal
« Reply #2 on: February 06, 2011, 05:31:33 am »
floattostrF(f1/f2, ffFixed, 3, 2); 

What does '3' stand for ?
And are there any more except '3' ?
Lazarus 1.2.4 / Win 32 / THAILAND

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Number with decimal
« Reply #3 on: February 06, 2011, 08:29:09 am »
Quote
What does '3' stand for ?
The last two arguments is the same as formatting output via Write(Ln). The 3rd is the minimum number of spaces while the last is the number of decimals.
Quote
And are there any more except '3' ?
Sure, you can adjust the spaces to your needs

 

TinyPortal © 2005-2018