Board index » delphi » Truncate Digits before point

Truncate Digits before point

Is there a way that the digits before the point can be truncated?
For example if a real variable Qty has a value of 1234567.891 then
format('%6.2f', [Qty]) returns 1234567.89 ie it correctly handles the
decimals but seems to expand the number of digits before the point. Is
this correct behavior or is there a workaround?
I'd expect it to return 123456.89 or 234567.89
 

Re:Truncate Digits before point


Quote
David Carle <d...@davidcarle.com> wrote:
>Is there a way that the digits before the point can be truncated?
>For example if a real variable Qty has a value of 1234567.891 then
>format('%6.2f', [Qty]) returns 1234567.89 ie it correctly handles the
>decimals but seems to expand the number of digits before the point. Is
>this correct behavior or is there a workaround?
>I'd expect it to return 123456.89 or 234567.89

I think it is correct behavior.  If it truncated before the
decial then it would give an erroneous value if it is too large
to fit in the field.

Re:Truncate Digits before point


Rounding number and changing value befor point is not the same. You need to
break number in two parts and change values by div or mod or delete for
strings (after inttostr or floattostr function), and then connect them
again.

Quote
David Carle <d...@davidcarle.com> wrote in message

news:3B3E924F.6E32@davidcarle.com...
Quote
> Is there a way that the digits before the point can be truncated?
> For example if a real variable Qty has a value of 1234567.891 then
> format('%6.2f', [Qty]) returns 1234567.89 ie it correctly handles the
> decimals but seems to expand the number of digits before the point. Is
> this correct behavior or is there a workaround?
> I'd expect it to return 123456.89 or 234567.89

Re:Truncate Digits before point


You could use string manipulation, e.g.

var
  S: String;
begin
  S := Format('%.2f', [Qty]);
  S := Copy(S, Length(S) - 8, 9);
end;

"David Carle" <d...@davidcarle.com> schreef in bericht
news:3B3E924F.6E32@davidcarle.com...

Quote
> Is there a way that the digits before the point can be truncated?
> For example if a real variable Qty has a value of 1234567.891 then
> format('%6.2f', [Qty]) returns 1234567.89 ie it correctly handles the
> decimals but seems to expand the number of digits before the point. Is
> this correct behavior or is there a workaround?
> I'd expect it to return 123456.89 or 234567.89

Other Threads