Repository navigation
Example 07 format like the game
github-actions[bot] edited this page Nov 21, 2026
·
1 revision
Generated from the thetowersdk repository. Edits made here are overwritten on the next push — change the source file instead.
/**
* Example 7 — Show numbers the way the game does.
*
* The Tower deals in numbers that run past 1e30, and it has its own conventions
* for shortening them. If your tool prints `1.4e21` where the game prints
* `1.40Sx`, players cannot check your answer against their screen — which is the
* only way they can trust it.
*
* This entry point is browser-safe and has no game-data dependency.
*
* Run it:
* pnpm exec tsx examples/07-format-like-the-game.ts
*/
import {
formatCoin,
formatDuration,
formatLargeNumber,
formatMultiplier,
parseDuration,
parseResource,
} from '@beardedunicorn/thetowersdk/formatting'
// Large numbers use the game's own suffix ladder, not scientific notation.
console.log('Coins as the game shows them:')
for (const value of [950, 12_500, 4.2e6, 8.9e9, 1.4e15, 7.7e21, 3.3e33]) {
console.log(` ${String(value).padEnd(10)} -> ${formatLargeNumber(value)}`)
}
console.log()
// `formatCoin` is the same ladder with the rounding a coin figure wants.
console.log(`A coin total: ${formatCoin(1.234e18)}`)
console.log(`A multiplier: ${formatMultiplier(13.4567)}`)
console.log()
// Durations round-trip. Lab research times arrive as "HH:MM:SS" strings in the
// catalogs, so parse before you add them together and format after.
console.log('Durations:')
const research = ['02:30:00', '18:45:30', '240:00:00']
let totalHours = 0
for (const text of research) {
const hours = parseDuration(text) / 3600
totalHours += hours
console.log(` ${text.padEnd(12)} -> ${hours.toFixed(2)}h -> ${formatDuration(hours * 3600)}`)
}
console.log(` total -> ${formatDuration(totalHours * 3600)}`)
console.log()
/*
* Going the other way matters more than it looks. Players paste values straight
* out of the game — "1.4Sx", "12.5K" — into whatever you build. `parseResource`
* reads that back to a number so you do not have to ask them to retype it, and
* whatever `formatLargeNumber` prints, this parses — the two round-trip.
*
* It returns `{ error: true }` rather than a number it guessed. That matters more
* than it sounds: "12abc" must not quietly become 12, and unparseable text must
* not become a 0 you cannot tell from a real one. Check `error` before `value`.
*/
console.log('Parsing what a player pasted in:')
for (const text of ['7.7s', '12.5K', '900', '3.3D', '12abc', 'not a number']) {
const parsed = parseResource(text)
const shown = parsed.error || parsed.value === undefined
? '(unparseable)'
: parsed.value.toExponential(3)
console.log(` ${text.padEnd(14)} -> ${shown}`)
}Generated from TheTowerSDK — do not edit here. Docs and live demos: https://beardedunicorn.github.io/TheTowerSDK/
- Workshop
- Workshop Enhancements
- Labs
- Cards
- Modules
- Module Rarities and Merging
- Module Substats
- Module Upgrade Costs
- Ultimate Weapons
- Ultimate Weapon Plus
- Bots
- Guardian
- Relics
- Themes
- Tiers
- Battle Conditions
- Enemies
- Perks
- Tournaments
- Milestones
- The Vault
- Daily Missions
- Guild
- Currencies
- Reference Tables
- Glossary
- Irreversible Upgrades
- Game Patch Notes
- Game Version History
SDK
- Quick Start
- Entry Points
- How It Fits Together
- Getting an Official Game Export
- Reading a Save
- Reading The Community Wiki
- Game 29.1 Data
- Formulas
- Builders
- Charts
- Effective Paths
- Examples
- Templates
- Building a Bot On This
- Google Sheets
- Desktop and Mobile
- Optional Add-ons
- Using It With An AI Agent
- Using Your Own Artwork
- Names And Acronyms
- Patch Notes
- Accuracy
- Versioning
- Where the Docs Live
- Contributing
- Credits
- License
Guides
Examples
- 01-browse-game-data.ts
- 02-read-a-save-file.ts
- 03-plan-upgrades-from-a-save.ts
- 04-generate-a-chart.ts
- 05-generate-a-cost-table.ts
- 06-read-the-community-wiki.ts
- 07-format-like-the-game.ts
- 08-build-a-calculator.ts
- 09-build-a-bot.ts
- 10-read-a-sheet.ts
- 11-build-a-knowledge-base.ts
- 12-show-module-and-card-art.ts
Templates