Re: [Lazarus] [fpc-pascal] G+ community for Free Pascal and Lazarus
Joined :-) On Tue, Dec 11, 2012 at 6:10 PM, silvioprog silviop...@gmail.com wrote: 2012/12/11 Graeme Geldenhuys gra...@geldenhuys.co.uk On 2012-12-11 15:41, Vincent Snijders wrote: In that case, I better not join. I'll be nice and say you can come play too. :) PS: members are up to 117 already!! Regards, - Graeme - -- fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ I invited members here in Brazil. \o/ -- Silvio Clécio My public projects - github.com/silvioprog -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
[Lazarus] Converting pence into pounds and then to string
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 Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] Converting pence into pounds and then to string
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) On Wed, Dec 12, 2012 at 12:23 PM, Richard Mace richard.m...@gmail.comwrote: 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 Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] Converting pence into pounds and then to string
On 2012-12-12 11:23, Richard Mace 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) Divide by 100. Then use FormatFloat() to output as string. Regards, - Graeme - -- fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] Converting pence into pounds and then to string
On Wed, Dec 12, 2012 at 11:23:46AM +, 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) Format('%d.%.2d', [Price div 100, Price mod 100]) Henry -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] Converting pence into pounds and then to string
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 Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] Converting pence into pounds and then to string
Guy's, Thanks for the quick info. It's brilliant to get so many good answers do quickly. Richard On 12 December 2012 12:17, Lukasz Sokol el.es...@gmail.com wrote: 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 Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] Converting pence into pounds and then to string
Am 12.12.2012 13:17 schrieb Lukasz Sokol el.es...@gmail.com: 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 -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] Converting pence into pounds and then to string
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; -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] Converting pence into pounds and then to string
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 purist, maybe Format('%s%%.%df',[currency, precision],[pennies/(10**precision)]); No ? :) Lukasz -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] Converting pence into pounds and then to string
On 12/12/2012 15:42, Lukasz Sokol wrote: 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! No ? :) Lukasz -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] Converting pence into pounds and then to string
On 12/12/2012 15:45, Lukasz Sokol wrote: Format('%s%%.%df',[currency, precision]),[pennies/(10**precision)]); Fat fingers L( Format(Format('%s%%.%df',[currency, precision]),[pennies/(10**precision)]); of course! No ? :) Lukasz -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] Converting pence into pounds and then to string
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; 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] -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] Converting pence into pounds and then to string
On Wed, Dec 12, 2012 at 11:23:46AM +, Richard Mace wrote: 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. -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus