Skip to content

Commit 04d5d16

Browse files
Add support for floating points ending in .0 (#10) (#316)
Add support for floating points ending in .0 in Python implementation. The current behavior just converts them into an integer. --------- Co-authored-by: Steve Rice <steve@steverice.org>
1 parent deb2e18 commit 04d5d16

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
changeKind: fix
3+
packages:
4+
- "@alloy-js/python"
5+
---
6+
7+
Add support for floats that end in .0

packages/python/src/components/Atom.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { For, Indent, memo } from "@alloy-js/core";
22

33
export interface AtomProps {
44
jsValue?: unknown;
5+
asFloat?: boolean;
56
}
67

78
/**
@@ -25,6 +26,9 @@ export function Atom(props: AtomProps): any {
2526
if (typeof jsValue === "undefined") {
2627
return "None";
2728
} else if (typeof jsValue === "number") {
29+
if (props.asFloat && Number.isInteger(jsValue)) {
30+
return jsValue.toFixed(1);
31+
}
2832
return String(jsValue);
2933
} else if (typeof jsValue === "boolean") {
3034
return jsValue ? "True" : "False";

packages/python/test/values.test.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,40 @@ describe("Atom", () => {
1515
expect(toSourceText([<py.Atom jsValue={123} />])).toRenderTo("123");
1616
});
1717

18+
it("renders floating point number", () => {
19+
expect(toSourceText([<py.Atom jsValue={123.456} />])).toRenderTo("123.456");
20+
});
21+
22+
it("renders floating point number when hinted", () => {
23+
expect(toSourceText([<py.Atom jsValue={123.456} asFloat />])).toRenderTo(
24+
"123.456",
25+
);
26+
});
27+
28+
it("renders floating point number with decimal point zero when hinted", () => {
29+
expect(toSourceText([<py.Atom jsValue={123.0} asFloat />])).toRenderTo(
30+
"123.0",
31+
);
32+
});
33+
34+
it("renders integer as float when hinted", () => {
35+
expect(toSourceText([<py.Atom jsValue={123} asFloat />])).toRenderTo(
36+
"123.0",
37+
);
38+
});
39+
40+
it("renders small positive float when hinted", () => {
41+
expect(toSourceText([<py.Atom jsValue={0.07} asFloat />])).toRenderTo(
42+
"0.07",
43+
);
44+
});
45+
46+
it("renders large positive numbers as float when hinted", () => {
47+
expect(toSourceText([<py.Atom jsValue={2 ** 64} asFloat />])).toRenderTo(
48+
"18446744073709551616.0",
49+
);
50+
});
51+
1852
it("renders boolean - True", () => {
1953
expect(toSourceText([<py.Atom jsValue={true} />])).toRenderTo("True");
2054
});

0 commit comments

Comments
 (0)