Skip to content

Commit

Permalink
Fix numeric 0 stringify producing 0.0 (#4135)
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheeguerin authored Aug 9, 2024
1 parent 7186ce4 commit 3558839
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
8 changes: 8 additions & 0 deletions .chronus/changes/fix-numeric-print-2024-7-9-16-3-25.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: fix
packages:
- "@typespec/compiler"
---

Fix numeric 0 stringify producing 0.0
4 changes: 3 additions & 1 deletion packages/compiler/src/core/numeric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ function parse(original: string): InternalData {
return { n, e: exp, s: sign, d: decimal };
}

function stringify(value: InternalData) {
function stringify(value: InternalData): string {
if (value.n === 0n) return "0";

const n = value.n.toString();
const sign = value.s === -1 ? "-" : "";
const int = value.e === 0 ? "0" : n.slice(0, value.e);
Expand Down
7 changes: 7 additions & 0 deletions packages/compiler/test/core/numeric.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ describe("parsing", () => {
});

describe("asString", () => {
it("0 is 0", () => {
expect(Numeric("0.0").toString()).toEqual("0");
});
it("1.0 is 1", () => {
expect(Numeric("1.0").toString()).toEqual("1");
});

it("doesn't include decimal if is an integer", () => {
expect(Numeric("123").toString()).toEqual("123");
});
Expand Down

0 comments on commit 3558839

Please sign in to comment.