Encountering JS Type Errors with the following format command: '{:.3g}'.format(123)

41 views Asked by At

I've encountered JavaScript TypeErrors when executing the following format command:

my_str = '{:.3g}'.format(123)

The errors occur when using the 'g' modifier, and where the formatting string specifies the same number of digits as the number being formatted, such as:

my_str = '{:.4g}'.format(1234)
my_str = '{:.5g}'.format(12345)

I used the following steps to test this issue:

I created the following Python file named format_issue_test.py:

my_str = '{:.3g}'.format(123)
print(my_str)

Ran the above file in Python 3 and got a result of '123'

I then transipled to JavaScript using:

transcrypt -b -sf format_issue_test

Created an HTML file to load the transpiled JavaScript:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script type="module">
    import './__target__/format_issue_test.js';
</script>
</body>
</html>

Opened the HTML file in a browser (FireFox) and checked the console. I expected to see an output of '123' in the console. But no result appeared, and the following TypeError shows up:

Uncaught TypeError: t is undefined
    format_float http://localhost:65432/transcrypt_test/format_issue_g_val_and_str_same_num_digits/__target__/org.transcrypt.__runtime__.js:17
    __format__ http://localhost:65432/transcrypt_test/format_issue_g_val_and_str_same_num_digits/__target__/org.transcrypt.__runtime__.js:23
    format http://localhost:65432/transcrypt_test/format_issue_g_val_and_str_same_num_digits/__target__/org.transcrypt.__runtime__.js:28
    get http://localhost:65432/transcrypt_test/format_issue_g_val_and_str_same_num_digits/__target__/org.transcrypt.__runtime__.js:53
    get http://localhost:65432/transcrypt_test/format_issue_g_val_and_str_same_num_digits/__target__/org.transcrypt.__runtime__.js:52
    __get__ http://localhost:65432/transcrypt_test/format_issue_g_val_and_str_same_num_digits/__target__/org.transcrypt.__runtime__.js:3
    <anonymous> http://localhost:65432/transcrypt_test/format_issue_g_val_and_str_same_num_digits/__target__/format_issue_test.js:3

I repeated the same steps, but changed the formatting command to:

my_str = '{:.3g}'.format(1234)

The TypeError no longer occurs, and the console shows the expected result of '1.23e+03'.

0

There are 0 answers