Skip to content

NETSCRIPT: Moved formatting functions to their own interface #1635

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions markdown/bitburner.bladeburner.nextupdate.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ The amount of real time spent asleep between updates can vary due to "bonus time
```js
while (true) {
const duration = await ns.bladeburner.nextUpdate();
ns.print(`Bladeburner Division completed ${ns.tFormat(duration)} of actions.`);
ns.print(`Bonus time remaining: ${ns.tFormat(ns.bladeburner.getBonusTime())}`);
ns.print(`Bladeburner Division completed ${ns.format.time(duration)} of actions.`);
ns.print(`Bonus time remaining: ${ns.format.time(ns.bladeburner.getBonusTime())}`);
// Manage the Bladeburner division
}
```
Expand Down
1 change: 1 addition & 0 deletions markdown/bitburner.codingcontractnameenumtype.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ type CodingContractNameEnumType = {
EncryptionICaesarCipher: "Encryption I: Caesar Cipher";
EncryptionIIVigenereCipher: "Encryption II: Vigenère Cipher";
SquareRoot: "Square Root";
TotalPrimesInRange: "Total Number of Primes";
};
```
1 change: 1 addition & 0 deletions markdown/bitburner.codingcontractsignatures.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ export type CodingContractSignatures = {
"Encryption I: Caesar Cipher": [[string, number], string];
"Encryption II: Vigenère Cipher": [[string, string], string];
"Square Root": [bigint, bigint, [string, string]];
"Total Number of Primes": [number[], number];
};
```
23 changes: 23 additions & 0 deletions markdown/bitburner.format.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [bitburner](./bitburner.md) &gt; [Format](./bitburner.format.md)

## Format interface

Format API

**Signature:**

```typescript
export interface Format
```

## Methods

| Method | Description |
| --- | --- |
| [number(n, fractionalDigits, suffixStart, isInteger)](./bitburner.format.number.md) | Format a number. |
| [percent(n, fractionalDigits, suffixStart)](./bitburner.format.percent.md) | Format a number as a percentage. |
| [ram(n, fractionalDigits)](./bitburner.format.ram.md) | Format a number as an amount of ram. |
| [time(milliseconds, milliPrecision)](./bitburner.format.time.md) | Format time to a readable string. |

Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [bitburner](./bitburner.md) &gt; [NS](./bitburner.ns.md) &gt; [formatNumber](./bitburner.ns.formatnumber.md)
[Home](./index.md) &gt; [bitburner](./bitburner.md) &gt; [Format](./bitburner.format.md) &gt; [number](./bitburner.format.number.md)

## NS.formatNumber() method
## Format.number() method

Format a number.

**Signature:**

```typescript
formatNumber(n: number, fractionalDigits?: number, suffixStart?: number, isInteger?: boolean): string;
number(n: number, fractionalDigits?: number, suffixStart?: number, isInteger?: boolean): string;
```

## Parameters
Expand All @@ -18,7 +18,7 @@ formatNumber(n: number, fractionalDigits?: number, suffixStart?: number, isInteg
| --- | --- | --- |
| n | number | Number to format. |
| fractionalDigits | number | _(Optional)_ Number of digits to show in the fractional part of the decimal number. Optional, defaults to 3. |
| suffixStart | number | _(Optional)_ How high a number must be before a suffix will be added. Optional, defaults to 1000. Must be greater than or equal to 1000 if specified. |
| suffixStart | number | _(Optional)_ How high a number must be before a suffix will be added. Optional, defaults to 1000. |
| isInteger | boolean | _(Optional)_ Whether the number represents an integer. Integers do not display fractional digits until a suffix is present. Optional, defaults to false. |

**Returns:**
Expand All @@ -31,7 +31,7 @@ Formatted number.

RAM cost: 0 GB

Converts a number into a numeric string with the specified format options. This is the same function that the game itself uses to display numbers. The format also depends on the Numeric Display settings (all options on the "Numeric Display" options page) To format ram or percentages, see [formatRam](./bitburner.ns.formatram.md) and [formatPercent](./bitburner.ns.formatpercent.md)<!-- -->.
Converts a number into a numeric string with the specified format options. This is the same function that the game itself uses to display numbers. The format also depends on the Numeric Display settings (all options on the "Numeric Display" options page) To format ram or percentages, see [format.ram](./bitburner.format.ram.md) and [format.percent](./bitburner.format.percent.md)

This function has some quirky undocumented behaviors. This is a non-exhaustive list of those behaviors:

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [bitburner](./bitburner.md) &gt; [NS](./bitburner.ns.md) &gt; [formatPercent](./bitburner.ns.formatpercent.md)
[Home](./index.md) &gt; [bitburner](./bitburner.md) &gt; [Format](./bitburner.format.md) &gt; [percent](./bitburner.format.percent.md)

## NS.formatPercent() method
## Format.percent() method

Format a number as a percentage.

**Signature:**

```typescript
formatPercent(n: number, fractionalDigits?: number, suffixStart?: number): string;
percent(n: number, fractionalDigits?: number, suffixStart?: number): string;
```

## Parameters
Expand All @@ -30,5 +30,5 @@ Formatted percentage.

RAM cost: 0 GB

Converts a number into a percentage string with the specified number of fractional digits. This is the same function that the game itself uses to display percentages. The format also depends on the Numeric Display settings (all options on the "Numeric Display" options page) To format plain numbers or ram, see [formatNumber](./bitburner.ns.formatnumber.md) and [formatRam](./bitburner.ns.formatram.md)
Converts a number into a percentage string with the specified number of fractional digits. This is the same function that the game itself uses to display percentages. The format also depends on the Numeric Display settings (all options on the "Numeric Display" options page) To format plain numbers or ram, see [format.number](./bitburner.format.number.md) and [format.ram](./bitburner.format.ram.md)

Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [bitburner](./bitburner.md) &gt; [NS](./bitburner.ns.md) &gt; [formatRam](./bitburner.ns.formatram.md)
[Home](./index.md) &gt; [bitburner](./bitburner.md) &gt; [Format](./bitburner.format.md) &gt; [ram](./bitburner.format.ram.md)

## NS.formatRam() method
## Format.ram() method

Format a number as an amount of ram.

**Signature:**

```typescript
formatRam(n: number, fractionalDigits?: number): string;
ram(n: number, fractionalDigits?: number): string;
```

## Parameters
Expand All @@ -29,5 +29,5 @@ Formatted ram amount.

RAM cost: 0 GB

Converts a number into a ram string with the specified number of fractional digits. This is the same function that the game itself uses to display ram. The format also depends on the Numeric Display settings (all options on the "Numeric Display" options page) To format plain numbers or percentages, see [formatNumber](./bitburner.ns.formatnumber.md) and [formatPercent](./bitburner.ns.formatpercent.md)
Converts a number into a ram string with the specified number of fractional digits. This is the same function that the game itself uses to display ram. The format also depends on the Numeric Display settings (all options on the "Numeric Display" options page) To format plain numbers or percentages, see [format.number](./bitburner.format.number.md) and [format.percent](./bitburner.format.percent.md)

Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [bitburner](./bitburner.md) &gt; [NS](./bitburner.ns.md) &gt; [tFormat](./bitburner.ns.tformat.md)
[Home](./index.md) &gt; [bitburner](./bitburner.md) &gt; [Format](./bitburner.format.md) &gt; [time](./bitburner.format.time.md)

## NS.tFormat() method
## Format.time() method

Format time to a readable string.

**Signature:**

```typescript
tFormat(milliseconds: number, milliPrecision?: boolean): string;
time(milliseconds: number, milliPrecision?: boolean): string;
```

## Parameters
Expand Down
4 changes: 2 additions & 2 deletions markdown/bitburner.gang.nextupdate.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ The amount of real time spent asleep between updates can vary due to "bonus time
```js
while (true) {
const duration = await ns.gang.nextUpdate();
ns.print(`Gang completed ${ns.tFormat(duration)} of activity.`);
ns.print(`Bonus time remaining: ${ns.tFormat(ns.gang.getBonusTime())}`);
ns.print(`Gang completed ${ns.format.time(duration)} of activity.`);
ns.print(`Bonus time remaining: ${ns.format.time(ns.gang.getBonusTime())}`);
// Manage the Gang
}
```
Expand Down
1 change: 1 addition & 0 deletions markdown/bitburner.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
| [Export](./bitburner.export.md) | Export order for a material |
| [FactionWorkTask](./bitburner.factionworktask.md) | Faction Work |
| [FileRequirement](./bitburner.filerequirement.md) | Player must have a specific Literature or Message file on their home computer. |
| [Format](./bitburner.format.md) | Format API |
| [Formulas](./bitburner.formulas.md) | Formulas API |
| [Fragment](./bitburner.fragment.md) | |
| [GameInfo](./bitburner.gameinfo.md) | Game Information |
Expand Down
18 changes: 18 additions & 0 deletions markdown/bitburner.ns.format.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [bitburner](./bitburner.md) &gt; [NS](./bitburner.ns.md) &gt; [format](./bitburner.ns.format.md)

## NS.format property

Namespace for formatting functions.

**Signature:**

```typescript
readonly format: Format;
```

## Remarks

RAM cost: 0 GB

2 changes: 1 addition & 1 deletion markdown/bitburner.ns.getpurchasedservercost.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ Returns the cost to purchase a server with the specified amount of ram.
```js
const ram = 2 ** 20;
const cost = ns.getPurchasedServerCost(ram);
ns.tprint(`A purchased server with ${ns.formatRam(ram)} costs $${ns.formatNumber(cost)}`);
ns.tprint(`A purchased server with ${ns.format.ram(ram)} costs $${ns.format.number(cost)}`);
```

5 changes: 1 addition & 4 deletions markdown/bitburner.ns.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export async function main(ns) {
| [codingcontract](./bitburner.ns.codingcontract.md) | <code>readonly</code> | [CodingContract](./bitburner.codingcontract.md) | Namespace for codingcontract functions. |
| [corporation](./bitburner.ns.corporation.md) | <code>readonly</code> | [Corporation](./bitburner.corporation.md) | Namespace for corporation functions. Contains spoilers. |
| [enums](./bitburner.ns.enums.md) | | [NSEnums](./bitburner.nsenums.md) | |
| [format](./bitburner.ns.format.md) | <code>readonly</code> | [Format](./bitburner.format.md) | Namespace for formatting functions. |
| [formulas](./bitburner.ns.formulas.md) | <code>readonly</code> | [Formulas](./bitburner.formulas.md) | Namespace for formulas functions. |
| [gang](./bitburner.ns.gang.md) | <code>readonly</code> | [Gang](./bitburner.gang.md) | Namespace for gang functions. Contains spoilers. |
| [go](./bitburner.ns.go.md) | <code>readonly</code> | [Go](./bitburner.go.md) | Namespace for Go functions. |
Expand Down Expand Up @@ -71,9 +72,6 @@ export async function main(ns) {
| [exit()](./bitburner.ns.exit.md) | Terminates the current script immediately. |
| [fileExists(filename, host)](./bitburner.ns.fileexists.md) | Check if a file exists. |
| [flags(schema)](./bitburner.ns.flags.md) | Parse command line flags. |
| [formatNumber(n, fractionalDigits, suffixStart, isInteger)](./bitburner.ns.formatnumber.md) | Format a number. |
| [formatPercent(n, fractionalDigits, suffixStart)](./bitburner.ns.formatpercent.md) | Format a number as a percentage. |
| [formatRam(n, fractionalDigits)](./bitburner.ns.formatram.md) | Format a number as an amount of ram. |
| [ftpcrack(host)](./bitburner.ns.ftpcrack.md) | Runs FTPCrack.exe on a server. |
| [getBitNodeMultipliers(n, lvl)](./bitburner.ns.getbitnodemultipliers.md) | Get the current BitNode multipliers. |
| [getFavorToDonate()](./bitburner.ns.getfavortodonate.md) | Returns the amount of Faction favor required to be able to donate to a faction. |
Expand Down Expand Up @@ -165,7 +163,6 @@ export async function main(ns) {
| [sprintf(format, args)](./bitburner.ns.sprintf.md) | Format a string. |
| [sqlinject(host)](./bitburner.ns.sqlinject.md) | Runs SQLInject.exe on a server. |
| [tail(fn, host, args)](./bitburner.ns.tail.md) | Open the tail window of a script. This function is deprecated and will be removed in a later version. |
| [tFormat(milliseconds, milliPrecision)](./bitburner.ns.tformat.md) | Format time to a readable string. |
| [toast(msg, variant, duration)](./bitburner.ns.toast.md) | Queue a toast (bottom-right notification). |
| [tprint(args)](./bitburner.ns.tprint.md) | Prints one or more values or variables to the Terminal. |
| [tprintf(format, values)](./bitburner.ns.tprintf.md) | Prints a raw value or a variable to the Terminal. |
Expand Down
2 changes: 1 addition & 1 deletion markdown/bitburner.singularity.getdarkwebprogramcost.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ If the program does not exist, an error is thrown.
```js
const programName = "BruteSSH.exe";
const cost = ns.singularity.getDarkwebProgramCost(programName);
if (cost > 0) ns.tprint(`${programName} costs $${ns.formatNumber(cost)}`);
if (cost > 0) ns.tprint(`${programName} costs $${ns.format.number(cost)}`);
```

12 changes: 8 additions & 4 deletions src/Netscript/RamCostGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,13 @@ const singularity = {
getCurrentWork: SF4Cost(0.5),
} as const;

const format = {
number: 0,
ram: 0,
percent: 0,
time: 0,
} as const;

// Gang API
const gang = {
createGang: RamCostConstants.GangApiBase / 4,
Expand Down Expand Up @@ -490,6 +497,7 @@ export const RamCosts: RamCostTree<NSFull> = {
hacknet,
stock,
singularity,
format,
gang,
go,
bladeburner,
Expand Down Expand Up @@ -599,10 +607,6 @@ export const RamCosts: RamCostTree<NSFull> = {
getScriptExpGain: RamCostConstants.GetScript,
getRunningScript: RamCostConstants.GetRunningScript,
ramOverride: 0,
formatNumber: 0,
formatRam: 0,
formatPercent: 0,
tFormat: 0,
prompt: 0,
wget: 0,
getFavorToDonate: RamCostConstants.GetFavorToDonate,
Expand Down
31 changes: 2 additions & 29 deletions src/NetscriptFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ import {
formatRam,
formatSecurity,
formatThreads,
formatNumber,
} from "./ui/formatNumber";
import { convertTimeMsToTimeElapsedString } from "./utils/StringHelperFunctions";
import { roundToTwo } from "./utils/helpers/roundToTwo";
Expand Down Expand Up @@ -114,6 +113,7 @@ import { canAccessBitNodeFeature, validBitNodes } from "./BitNode/BitNodeUtils";
import { isIPAddress } from "./Types/strings";
import { compile } from "./NetscriptJSEvaluator";
import { Script } from "./Script/Script";
import { NetscriptFormat } from "./NetscriptFunctions/Format";

export const enums: NSEnums = {
CityName,
Expand All @@ -136,6 +136,7 @@ export type NSFull = Readonly<Omit<NS & INetscriptExtra, "pid" | "args" | "enums

export const ns: InternalAPI<NSFull> = {
singularity: NetscriptSingularity(),
format: NetscriptFormat(),
gang: NetscriptGang(),
go: NetscriptGo(),
bladeburner: NetscriptBladeburner(),
Expand Down Expand Up @@ -1613,34 +1614,6 @@ export const ns: InternalAPI<NSFull> = {
}
return runningScript.onlineExpGained / runningScript.onlineRunningTime;
},
formatNumber:
(ctx) =>
(_n, _fractionalDigits = 3, _suffixStart = 1000, isInteger) => {
const n = helpers.number(ctx, "n", _n);
const fractionalDigits = helpers.number(ctx, "fractionalDigits", _fractionalDigits);
const suffixStart = helpers.number(ctx, "suffixStart", _suffixStart);
return formatNumber(n, fractionalDigits, suffixStart, !!isInteger);
},
formatRam:
(ctx) =>
(_n, _fractionalDigits = 2) => {
const n = helpers.number(ctx, "n", _n);
const fractionalDigits = helpers.number(ctx, "fractionalDigits", _fractionalDigits);
return formatRam(n, fractionalDigits);
},
formatPercent:
(ctx) =>
(_n, _fractionalDigits = 2, _multStart = 1e6) => {
const n = helpers.number(ctx, "n", _n);
const fractionalDigits = helpers.number(ctx, "fractionalDigits", _fractionalDigits);
const multStart = helpers.number(ctx, "multStart", _multStart);
return formatPercent(n, fractionalDigits, multStart);
},
tFormat: (ctx) => (_milliseconds, _milliPrecision) => {
const milliseconds = helpers.number(ctx, "milliseconds", _milliseconds);
const milliPrecision = !!_milliPrecision;
return convertTimeMsToTimeElapsedString(milliseconds, milliPrecision);
},
alert: (ctx) => (_message) => {
const message = helpers.string(ctx, "message", _message);
dialogBoxCreate(message, { html: true, canBeDismissedEasily: true });
Expand Down
38 changes: 38 additions & 0 deletions src/NetscriptFunctions/Format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Format } from "@nsdefs";
import { InternalAPI } from "src/Netscript/APIWrapper";
import { helpers } from "../Netscript/NetscriptHelpers";
import { formatNumber, formatPercent, formatRam } from "../ui/formatNumber";
import { convertTimeMsToTimeElapsedString } from "../utils/StringHelperFunctions";

export function NetscriptFormat(): InternalAPI<Format> {
return {
number:
(ctx) =>
(_n, _fractionalDigits = 3, _suffixStart = 1000, isInteger) => {
const n = helpers.number(ctx, "n", _n);
const fractionalDigits = helpers.number(ctx, "fractionalDigits", _fractionalDigits);
const suffixStart = helpers.number(ctx, "suffixStart", _suffixStart);
return formatNumber(n, fractionalDigits, suffixStart, !!isInteger);
},
ram:
(ctx) =>
(_n, _fractionalDigits = 2) => {
const n = helpers.number(ctx, "n", _n);
const fractionalDigits = helpers.number(ctx, "fractionalDigits", _fractionalDigits);
return formatRam(n, fractionalDigits);
},
percent:
(ctx) =>
(_n, _fractionalDigits = 2, _multStart = 1e6) => {
const n = helpers.number(ctx, "n", _n);
const fractionalDigits = helpers.number(ctx, "fractionalDigits", _fractionalDigits);
const multStart = helpers.number(ctx, "multStart", _multStart);
return formatPercent(n, fractionalDigits, multStart);
},
time: (ctx) => (_milliseconds, _milliPrecision) => {
const milliseconds = helpers.number(ctx, "milliseconds", _milliseconds);
const milliPrecision = !!_milliPrecision;
return convertTimeMsToTimeElapsedString(milliseconds, milliPrecision);
},
};
}
Loading