Skip to content
This repository has been archived by the owner on Dec 31, 2024. It is now read-only.

Commit

Permalink
feat: add Number.toFixed
Browse files Browse the repository at this point in the history
  • Loading branch information
TopchetoEU committed Apr 3, 2024
1 parent a8775d2 commit 71b4024
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/java/me/topchetoeu/jscript/lib/NumberLib.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package me.topchetoeu.jscript.lib;

import java.text.NumberFormat;

import me.topchetoeu.jscript.runtime.values.ObjectValue;
import me.topchetoeu.jscript.runtime.values.Values;
import me.topchetoeu.jscript.utils.interop.Arguments;
Expand Down Expand Up @@ -85,6 +87,14 @@ public static double __parseInt(Arguments args) {
@Expose public static String __toString(Arguments args) {
return Values.toString(args.ctx, args.self);
}
@Expose public static String __toFixed(Arguments args) {
var digits = args.getInt(0, 0);

var nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(digits);

return nf.format(args.getDouble(-1));
}
@Expose public static double __valueOf(Arguments args) {
if (Values.isWrapper(args.self, NumberLib.class)) return Values.wrapper(args.self, NumberLib.class).value;
else return Values.toNumber(args.ctx, args.self);
Expand Down

0 comments on commit 71b4024

Please sign in to comment.