Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get value after decimal separator from floating point number in Delphi

How to get value after decimal separator for given floating point number?

Number: 129.60
Expected result: 60
like image 733
a.ilic Avatar asked Oct 29 '12 20:10

a.ilic


People also ask

How do you get the float value upto 2 decimal places?

format("%. 2f", 1.23456); This will format the floating point number 1.23456 up-to 2 decimal places, because we have used two after decimal point in formatting instruction %.

Can float take decimal values?

float has 7 decimal digits of precision.

What is the number of places of precision in a float datatype?

The data type float has 24 bits of precision. This is equivalent to only about 7 decimal places. (The rest of the 32 bits are used for the sign and size of the number.)


2 Answers

Just use the Frac function.

For instance, Frac(3.14) equals 0.14. Of course, as soon as you have got rid of the integer part of the number, you can use any method of your choice to make a string out of it, like FloatToStr, FormatFloat, Format, etc.

like image 119
Andreas Rejbrand Avatar answered Sep 22 '22 13:09

Andreas Rejbrand


var
  abobrinha, Expected : real;
begin
  abobrinha := 129.60;
  Expected := abobrinha - trunc(abobrinha);
end;
like image 42
Henrique Ramos Avatar answered Sep 19 '22 13:09

Henrique Ramos