[Pas2js] Built-in helper methods for Double type

Michael Van Canneyt michael at freepascal.org
Wed Sep 4 13:27:47 CEST 2019


Why not simply use type helpers ?

TDoubleHelper = type helper for Double
   function toString(radix: integer = 0): String;
   function toFixed(fractionDigits: integer = 0): string;
   function toExponential(fractionDigits: integer = 0): string;
   function toPrecision(precision: integer): string;
end;


It's on my TODO list to port the type helpers of FPC sysutils to pas2js.
But this is low priority, since I don't use type helpers myself.

If you feel like porting them yourself, I'll be glad to help out with making
a patch :-)

Michael.



On Wed, 4 Sep 2019, warleyalex via Pas2js wrote:

> I came across this situation, when translating to pas2js:
>
> // Determine X and cut to 2 decimals
>  x = (Math.sin(rad) * r).toFixed(2);
>
> It would be nice if pas2js could extend the built-in Number methods for the
> type Double, Integer, for instance: syntax something like:
>
> var
>  x: string;
>  r, rad: Double;
> begin
>   x:= (sin(rad) * r).toFixed(2);
>
> struggling how to achieve this situation, an workaround would be:
>
> I had to define an external class at the system unit, math section and a
> public single function:
>
> type
>  TJSNumber = class external name 'number'
>    function toString(radix: integer = 0): String;
>    function toFixed(fractionDigits: integer = 0): string;
>    function toExponential(fractionDigits: integer = 0): string;
>    function toPrecision(precision: integer): string;
>  end;
>
> function NumberTo(const aNumber: Double): TJSNumber; external name ' ';
>
> and then
>
> // Determine X and cut to 2 decimals
> x := NumberTo(sin(10.1) * 2.5).toFixed(2); // $mod.x =  (Math.sin(10.1) *
> 2.5).toFixed(2);
>
>
>
>
> --
> Sent from: http://pas2js.38893.n8.nabble.com/
> _______________________________________________
> Pas2js maillist  -  Pas2js at lists.freepascal.org
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/pas2js


More information about the Pas2js mailing list