diff --git a/markdown/bitburner.bladeburner.nextupdate.md b/markdown/bitburner.bladeburner.nextupdate.md index 19c29f394..fcb3c7175 100644 --- a/markdown/bitburner.bladeburner.nextupdate.md +++ b/markdown/bitburner.bladeburner.nextupdate.md @@ -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 } ``` diff --git a/markdown/bitburner.codingcontractnameenumtype.md b/markdown/bitburner.codingcontractnameenumtype.md index 3e09bea13..6c8805489 100644 --- a/markdown/bitburner.codingcontractnameenumtype.md +++ b/markdown/bitburner.codingcontractnameenumtype.md @@ -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"; }; ``` diff --git a/markdown/bitburner.codingcontractsignatures.md b/markdown/bitburner.codingcontractsignatures.md index 3b5e5a151..597f5b019 100644 --- a/markdown/bitburner.codingcontractsignatures.md +++ b/markdown/bitburner.codingcontractsignatures.md @@ -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]; }; ``` diff --git a/markdown/bitburner.format.md b/markdown/bitburner.format.md new file mode 100644 index 000000000..10feca43d --- /dev/null +++ b/markdown/bitburner.format.md @@ -0,0 +1,23 @@ + + +[Home](./index.md) > [bitburner](./bitburner.md) > [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. | + diff --git a/markdown/bitburner.ns.formatnumber.md b/markdown/bitburner.format.number.md similarity index 75% rename from markdown/bitburner.ns.formatnumber.md rename to markdown/bitburner.format.number.md index 8e43643f4..7c1c42524 100644 --- a/markdown/bitburner.ns.formatnumber.md +++ b/markdown/bitburner.format.number.md @@ -1,15 +1,15 @@ -[Home](./index.md) > [bitburner](./bitburner.md) > [NS](./bitburner.ns.md) > [formatNumber](./bitburner.ns.formatnumber.md) +[Home](./index.md) > [bitburner](./bitburner.md) > [Format](./bitburner.format.md) > [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 @@ -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:** @@ -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: diff --git a/markdown/bitburner.ns.formatpercent.md b/markdown/bitburner.format.percent.md similarity index 69% rename from markdown/bitburner.ns.formatpercent.md rename to markdown/bitburner.format.percent.md index 74945f62d..aa641c5fe 100644 --- a/markdown/bitburner.ns.formatpercent.md +++ b/markdown/bitburner.format.percent.md @@ -1,15 +1,15 @@ -[Home](./index.md) > [bitburner](./bitburner.md) > [NS](./bitburner.ns.md) > [formatPercent](./bitburner.ns.formatpercent.md) +[Home](./index.md) > [bitburner](./bitburner.md) > [Format](./bitburner.format.md) > [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 @@ -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) diff --git a/markdown/bitburner.ns.formatram.md b/markdown/bitburner.format.ram.md similarity index 69% rename from markdown/bitburner.ns.formatram.md rename to markdown/bitburner.format.ram.md index d5b0ae71d..e65cbfed5 100644 --- a/markdown/bitburner.ns.formatram.md +++ b/markdown/bitburner.format.ram.md @@ -1,15 +1,15 @@ -[Home](./index.md) > [bitburner](./bitburner.md) > [NS](./bitburner.ns.md) > [formatRam](./bitburner.ns.formatram.md) +[Home](./index.md) > [bitburner](./bitburner.md) > [Format](./bitburner.format.md) > [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 @@ -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) diff --git a/markdown/bitburner.ns.tformat.md b/markdown/bitburner.format.time.md similarity index 68% rename from markdown/bitburner.ns.tformat.md rename to markdown/bitburner.format.time.md index 54b06b02a..1ceb7923e 100644 --- a/markdown/bitburner.ns.tformat.md +++ b/markdown/bitburner.format.time.md @@ -1,15 +1,15 @@ -[Home](./index.md) > [bitburner](./bitburner.md) > [NS](./bitburner.ns.md) > [tFormat](./bitburner.ns.tformat.md) +[Home](./index.md) > [bitburner](./bitburner.md) > [Format](./bitburner.format.md) > [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 diff --git a/markdown/bitburner.gang.nextupdate.md b/markdown/bitburner.gang.nextupdate.md index e3b3aedf2..6003f6fab 100644 --- a/markdown/bitburner.gang.nextupdate.md +++ b/markdown/bitburner.gang.nextupdate.md @@ -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 } ``` diff --git a/markdown/bitburner.md b/markdown/bitburner.md index a680279df..db870ab0a 100644 --- a/markdown/bitburner.md +++ b/markdown/bitburner.md @@ -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 | diff --git a/markdown/bitburner.ns.format.md b/markdown/bitburner.ns.format.md new file mode 100644 index 000000000..43e87a5f0 --- /dev/null +++ b/markdown/bitburner.ns.format.md @@ -0,0 +1,18 @@ + + +[Home](./index.md) > [bitburner](./bitburner.md) > [NS](./bitburner.ns.md) > [format](./bitburner.ns.format.md) + +## NS.format property + +Namespace for formatting functions. + +**Signature:** + +```typescript +readonly format: Format; +``` + +## Remarks + +RAM cost: 0 GB + diff --git a/markdown/bitburner.ns.getpurchasedservercost.md b/markdown/bitburner.ns.getpurchasedservercost.md index 75b8f112d..6c8fd7442 100644 --- a/markdown/bitburner.ns.getpurchasedservercost.md +++ b/markdown/bitburner.ns.getpurchasedservercost.md @@ -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)}`); ``` diff --git a/markdown/bitburner.ns.md b/markdown/bitburner.ns.md index 4e218cec8..609563347 100644 --- a/markdown/bitburner.ns.md +++ b/markdown/bitburner.ns.md @@ -36,6 +36,7 @@ export async function main(ns) { | [codingcontract](./bitburner.ns.codingcontract.md) | readonly | [CodingContract](./bitburner.codingcontract.md) | Namespace for codingcontract functions. | | [corporation](./bitburner.ns.corporation.md) | readonly | [Corporation](./bitburner.corporation.md) | Namespace for corporation functions. Contains spoilers. | | [enums](./bitburner.ns.enums.md) | | [NSEnums](./bitburner.nsenums.md) | | +| [format](./bitburner.ns.format.md) | readonly | [Format](./bitburner.format.md) | Namespace for formatting functions. | | [formulas](./bitburner.ns.formulas.md) | readonly | [Formulas](./bitburner.formulas.md) | Namespace for formulas functions. | | [gang](./bitburner.ns.gang.md) | readonly | [Gang](./bitburner.gang.md) | Namespace for gang functions. Contains spoilers. | | [go](./bitburner.ns.go.md) | readonly | [Go](./bitburner.go.md) | Namespace for Go functions. | @@ -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. | @@ -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. | diff --git a/markdown/bitburner.singularity.getdarkwebprogramcost.md b/markdown/bitburner.singularity.getdarkwebprogramcost.md index f4b98a390..76474d573 100644 --- a/markdown/bitburner.singularity.getdarkwebprogramcost.md +++ b/markdown/bitburner.singularity.getdarkwebprogramcost.md @@ -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)}`); ``` diff --git a/src/Netscript/RamCostGenerator.ts b/src/Netscript/RamCostGenerator.ts index 57f7fcc66..f1716918d 100644 --- a/src/Netscript/RamCostGenerator.ts +++ b/src/Netscript/RamCostGenerator.ts @@ -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, @@ -490,6 +497,7 @@ export const RamCosts: RamCostTree = { hacknet, stock, singularity, + format, gang, go, bladeburner, @@ -599,10 +607,6 @@ export const RamCosts: RamCostTree = { getScriptExpGain: RamCostConstants.GetScript, getRunningScript: RamCostConstants.GetRunningScript, ramOverride: 0, - formatNumber: 0, - formatRam: 0, - formatPercent: 0, - tFormat: 0, prompt: 0, wget: 0, getFavorToDonate: RamCostConstants.GetFavorToDonate, diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts index da443304d..774dca5a3 100644 --- a/src/NetscriptFunctions.ts +++ b/src/NetscriptFunctions.ts @@ -62,7 +62,6 @@ import { formatRam, formatSecurity, formatThreads, - formatNumber, } from "./ui/formatNumber"; import { convertTimeMsToTimeElapsedString } from "./utils/StringHelperFunctions"; import { roundToTwo } from "./utils/helpers/roundToTwo"; @@ -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, @@ -136,6 +136,7 @@ export type NSFull = Readonly = { singularity: NetscriptSingularity(), + format: NetscriptFormat(), gang: NetscriptGang(), go: NetscriptGo(), bladeburner: NetscriptBladeburner(), @@ -1613,34 +1614,6 @@ export const ns: InternalAPI = { } 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 }); diff --git a/src/NetscriptFunctions/Format.ts b/src/NetscriptFunctions/Format.ts new file mode 100644 index 000000000..c87a59a7e --- /dev/null +++ b/src/NetscriptFunctions/Format.ts @@ -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 { + 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); + }, + }; +} diff --git a/src/ScriptEditor/NetscriptDefinitions.d.ts b/src/ScriptEditor/NetscriptDefinitions.d.ts index 7c32c7755..dc9182de3 100644 --- a/src/ScriptEditor/NetscriptDefinitions.d.ts +++ b/src/ScriptEditor/NetscriptDefinitions.d.ts @@ -2772,7 +2772,7 @@ export interface Singularity { * ```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)}`); * ``` * @param programName - Name of program to check the price of * @returns Price of the specified darkweb program @@ -3866,8 +3866,8 @@ export interface Bladeburner { * ```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 * } * ``` @@ -4013,6 +4013,85 @@ export interface CodingContract { getContractTypes(): CodingContractName[]; } +/** + * Format API + * @public + */ +export interface Format { + /** + * Format a number. + * @remarks + * 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 {@link Format.ram | format.ram} and {@link Format.percent | format.percent} + * + * This function has some quirky undocumented behaviors. This is a non-exhaustive list of those behaviors: + * + * - "Infinity" and "-Infinity" are returned as "∞" and "-∞", respectively. + * + * - If you disable the suffix form in the settings page or the absolute value is greater than or equal to 1e33, this + * function will use the exponential form. This means that, if Math.abs(n) >= 1e33, the returned value is always in + * the exponential form, regardless of the setting. + * + * Note that the behaviors listed above are "undocumented", in the sense that we don't make any guarantee about + * backward compatibility. You must not rely on those behaviors. + * + * @param n - Number to format. + * @param fractionalDigits - Number of digits to show in the fractional part of the decimal number. Optional, defaults to 3. + * @param suffixStart - How high a number must be before a suffix will be added. Optional, defaults to 1000. + * @param isInteger - Whether the number represents an integer. Integers do not display fractional digits until a suffix is present. Optional, defaults to false. + * @returns Formatted number. + */ + number(n: number, fractionalDigits?: number, suffixStart?: number, isInteger?: boolean): string; + + /** + * Format a number as an amount of ram. + * @remarks + * 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 {@link Format.number | format.number} and {@link Format.percent | format.percent} + * + * @param n - Number to format as an amount of ram, in base units of GB (or GiB if that Numeric Display option is set). + * @param fractionalDigits - Number of digits to show in the fractional part of the decimal number. Optional, defaults to 2. + * @returns Formatted ram amount. + */ + ram(n: number, fractionalDigits?: number): string; + + /** + * Format a number as a percentage. + * @remarks + * 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 {@link Format.number | format.number} and {@link Format.ram | format.ram} + * + * @param n - Number to format as a percentage. + * @param fractionalDigits - Number of digits to show in the fractional part of the decimal number. Optional, defaults to 2. + * @param suffixStart - When to switch the percentage to a multiplier. Default is 1e6 or x1.00m. + * @returns Formatted percentage. + */ + percent(n: number, fractionalDigits?: number, suffixStart?: number): string; + + /** + * Format time to a readable string. + * @remarks + * RAM cost: 0 GB + * + * @param milliseconds - Number of millisecond to format. + * @param milliPrecision - Format time with subsecond precision. Defaults to false. + * @returns The formatted time. + */ + time(milliseconds: number, milliPrecision?: boolean): string; +} + /** * Gang API * @remarks @@ -4341,8 +4420,8 @@ export interface Gang { * ```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 * } * ``` @@ -6047,6 +6126,12 @@ export interface NS { */ readonly codingcontract: CodingContract; + /** + * Namespace for formatting functions. + * @remarks RAM cost: 0 GB + */ + readonly format: Format; + /** * Namespace for gang functions. Contains spoilers. * @remarks RAM cost: 0 GB @@ -7528,7 +7613,7 @@ export interface NS { * ```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)}`); * ``` * @param ram - Amount of RAM of a potential purchased server, in GB. Must be a power of 2 (2, 4, 8, 16, etc.). Maximum value of 1048576 (2^20). * @returns The cost to purchase a server with the specified amount of ram, or returns Infinity if ram is not a valid amount. @@ -7991,79 +8076,6 @@ export interface NS { */ vsprintf(format: string, args: any[]): string; - /** - * Format a number. - * @remarks - * 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 {@link NS.formatRam | formatRam} and {@link NS.formatPercent | formatPercent}. - * - * This function has some quirky undocumented behaviors. This is a non-exhaustive list of those behaviors: - * - * - "Infinity" and "-Infinity" are returned as "∞" and "-∞", respectively. - * - * - If you disable the suffix form in the settings page or the absolute value is greater than or equal to 1e33, this - * function will use the exponential form. This means that, if Math.abs(n) >= 1e33, the returned value is always in - * the exponential form, regardless of the setting. - * - * Note that the behaviors listed above are "undocumented", in the sense that we don't make any guarantee about - * backward compatibility. You must not rely on those behaviors. - * - * @param n - Number to format. - * @param fractionalDigits - Number of digits to show in the fractional part of the decimal number. Optional, defaults to 3. - * @param suffixStart - 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. - * @param isInteger - Whether the number represents an integer. Integers do not display fractional digits until a suffix is present. Optional, defaults to false. - * @returns Formatted number. - */ - formatNumber(n: number, fractionalDigits?: number, suffixStart?: number, isInteger?: boolean): string; - - /** - * Format a number as an amount of ram. - * @remarks - * 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 {@link NS.formatNumber | formatNumber} and {@link NS.formatPercent | formatPercent} - * - * @param n - Number to format as an amount of ram, in base units of GB (or GiB if that Numeric Display option is set). - * @param fractionalDigits - Number of digits to show in the fractional part of the decimal number. Optional, defaults to 2. - * @returns Formatted ram amount. - */ - formatRam(n: number, fractionalDigits?: number): string; - - /** - * Format a number as a percentage. - * @remarks - * 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 {@link NS.formatNumber | formatNumber} and {@link NS.formatRam | formatRam} - * - * @param n - Number to format as a percentage. - * @param fractionalDigits - Number of digits to show in the fractional part of the decimal number. Optional, defaults to 2. - * @param suffixStart - When to switch the percentage to a multiplier. Default is 1e6 or x1.00m. - * @returns Formatted percentage. - */ - formatPercent(n: number, fractionalDigits?: number, suffixStart?: number): string; - - /** - * Format time to a readable string. - * @remarks - * RAM cost: 0 GB - * - * @param milliseconds - Number of millisecond to format. - * @param milliPrecision - Format time with subsecond precision. Defaults to false. - * @returns The formatted time. - */ - tFormat(milliseconds: number, milliPrecision?: boolean): string; - /** * Prompt the player with an input modal. * @remarks diff --git a/src/utils/APIBreaks/3.0.0.ts b/src/utils/APIBreaks/3.0.0.ts index 837721b44..3a55455d1 100644 --- a/src/utils/APIBreaks/3.0.0.ts +++ b/src/utils/APIBreaks/3.0.0.ts @@ -60,4 +60,17 @@ export const breakInfos300: APIBreakInfo[] = [ 'It has been automatically replaced with "ns.corporation.getCorporation().nextState"', showPopUp: false, }, + { + brokenAPIs: [ + { name: "ns.formatNumber", migration: { searchValue: "ns.formatNumber", replaceValue: "ns.format.number" } }, + { name: "ns.formatRam", migration: { searchValue: "ns.formatRam", replaceValue: "ns.format.ram" } }, + { name: "ns.formatPercent", migration: { searchValue: "ns.formatPercent", replaceValue: "ns.format.percent" } }, + { name: "ns.tFormat", migration: { searchValue: "ns.tFormat", replaceValue: "ns.format.time" } }, + ], + info: + "The formatting functions have been moved to their own interface, ns.format.\n" + + "Each function has been replaced with their corresponding interface variant.\n" + + "Additionally, the naming of ns.tFormat has been changed to ns.format.time.", + showPopUp: false, + }, ];