Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2012-12

Converting pence into pounds and then to string

13 messages · 9 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Kostas Michalopoulos
to Richard Mace

Perhaps not the most efficient solution but that would do:

// p is the integer, f.e. 208
IntToStr(p div 100) + '.' + IntToStr((p mod 100) div 10) + IntToStr(p mod 10)

Richard Mace wrote 18 lines
Hi All, Got a simple question that has stumped me, which tends to happen if I don't do development for a couple of months. How's the best way of converting an integer number "208" (that is pence) into a string value that is in pounds (2.08) Thanks as always in advance. Richard -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20121212/a92a4844/attachment.html>

Reply by email

Lukasz Sokol
to Richard Mace: Got a simple question that has stumped me, which tends to happen if I don't do…
Richard Mace wrote 11 lines
Hi All, Got a simple question that has stumped me, which tends to happen if I don't do development for a couple of months. How's the best way of converting an integer number "208" (that is pence) into a string value that is in pounds (2.08) Thanks as always in advance. Richard

-ENOTENOUGHINFO (do you assume 2 decimal places always?)

but simplest variable precision would probably be

function PenceToString(ANumber: integer; APrecision : integer = 2):string
begin
  Result := Format('%.'+IntToStr(APrecision)'f',[ANumber div (10**APrecision)]);
end;

:] (assuming the ** operator actually works)

L.

Reply by email

Richard Mace
to Lukasz Sokol

Guy's,
Thanks for the quick info. It's brilliant to get so many good answers do quickly.

Richard

Lukasz Sokol wrote 33 lines
On 12/12/2012 11:23, Richard Mace wrote: > Hi All, Got a simple question that has stumped me, which tends to > happen if I don't do development for a couple of months. > > How's the best way of converting an integer number "208" (that is > pence) into a string value that is in pounds (2.08) > > Thanks as always in advance. > > Richard > > -ENOTENOUGHINFO (do you assume 2 decimal places always?) but simplest variable precision would probably be function PenceToString(ANumber: integer; APrecision : integer = 2):string begin Result := Format('%.'+IntToStr(APrecision)'f',[ANumber div (10**APrecision)]); end; :] (assuming the ** operator actually works) L. -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20121212/85098cc7/attachment.html>

Reply by email

Sven Barth
to Lukasz Sokol: -ENOTENOUGHINFO (do you assume 2 decimal places always?) but simplest variable precision…
"Lukasz Sokol" wrote 20 lines
On 12/12/2012 11:23, Richard Mace wrote: > Hi All, Got a simple question that has stumped me, which tends to > happen if I don't do development for a couple of months. > > How's the best way of converting an integer number "208" (that is > pence) into a string value that is in pounds (2.08) > > Thanks as always in advance. > > Richard > > -ENOTENOUGHINFO (do you assume 2 decimal places always?) but simplest variable precision would probably be function PenceToString(ANumber: integer; APrecision : integer = 2):string begin Result := Format('%.'+IntToStr(APrecision)'f',[ANumber div
(10**APrecision)]);
end;

The ** operator for floating point (and integers) is implemented in unit Math.

Regards,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20121212/eda4ec34/attachment.html>

Reply by email

Howard Page-Clark
to Sven Barth
> > How's the best way of converting an integer number "208" (that is > > pence) into a string value that is in pounds (2.08)

Here's another variant that provides an optional $, ? or ?

uses math;
function PenceToString(pennies: integer; precision: integer=2;
                        currency: string=''): string;
begin
  Result:= Format(Format(currency+'%%.%df',[precision]),
                  [pennies/(10**precision)]);
end;

Reply by email

Lukasz Sokol
to Howard Page-Clark
Howard Page-Clark wrote 13 lines
> > > How's the best way of converting an integer number "208" (that is > > > pence) into a string value that is in pounds (2.08) Here's another variant that provides an optional $, ? or ? uses math; function PenceToString(pennies: integer; precision: integer=2; currency: string=''): string; begin Result:= Format(Format(currency+'%%.%df',[precision]), [pennies/(10**precision)]); end;
Being purist, maybe
Format('%s%%.%df',[currency, precision],[pennies/(10**precision)]);

No ?

:)
Lukasz

Reply by email

Lukasz Sokol
to Lukasz Sokol
Lukasz Sokol wrote 18 lines
On 12/12/2012 15:21, Howard Page-Clark wrote: >> > > How's the best way of converting an integer number "208" (that is >> > > pence) into a string value that is in pounds (2.08) > > Here's another variant that provides an optional $, ? or ? > > uses math; > function PenceToString(pennies: integer; precision: integer=2; > currency: string=''): string; > begin > Result:= Format(Format(currency+'%%.%df',[precision]), > [pennies/(10**precision)]); > end; > Being [Format] purist, maybe Format('%s%%.%df',[currency, precision],[pennies/(10**precision)]);
  Format('%s%%.%df',[currency, precision]),[pennies/(10**precision)]);
                                         ^

of course!

Show quoted text 5 lines
No ? :) Lukasz

Reply by email

Lukasz Sokol
to Lukasz Sokol
Lukasz Sokol wrote
Format('%s%%.%df',[currency, precision]),[pennies/(10**precision)]);

Fat fingers L(

Format(Format('%s%%.%df',[currency, precision]),[pennies/(10**precision)]);
Show quoted text 7 lines
of course! > No ? > > :) > Lukasz >

Reply by email

Mark Morgan Lloyd
to Howard Page-Clark: Here's another variant that provides an optional $, ? or ? uses math; function…
Howard Page-Clark wrote 12 lines
> > > How's the best way of converting an integer number "208" (that is > > > pence) into a string value that is in pounds (2.08) Here's another variant that provides an optional $, ? or ? uses math; function PenceToString(pennies: integer; precision: integer=2; currency: string=''): string; begin Result:= Format(Format(currency+'%%.%df',[precision]), [pennies/(10**precision)]); end;

Possibly being careful about the encoding of that currency symbol. I'd also point out that everybody's assuming that OP is asking about post-decimalisation pennies :-)

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Marco van de Voort
to Richard Mace: Got a simple question that has stumped me, which tends to happen if I don't do…
Richard Mace wrote 5 lines
Got a simple question that has stumped me, which tends to happen if I don't do development for a couple of months. How's the best way of converting an integer number "208" (that is pence) into a string value that is in pounds (2.08)

Logically, to format currency one uses the standard library function formatcurr:

http://www.freepascal.org/docs-html/rtl/sysutils/formatcurr.html

uses sysutils;
var v : currency;
begin
 v:=208 /100;
 writeln(formatcurr('#.##',v));
end.

Reply by email