Skip to content

Add basic number formatting#4461

Closed
notJoon wants to merge 3 commits into
RustPython:mainfrom
notJoon:add-n-formatting
Closed

Add basic number formatting#4461
notJoon wants to merge 3 commits into
RustPython:mainfrom
notJoon:add-n-formatting

Conversation

@notJoon
Copy link
Copy Markdown
Contributor

@notJoon notJoon commented Jan 19, 2023

Description

Only basic functionalities have been implemented so that formatting can be applied and output even when a 'float' type number comes in.

However, not all functions are fully functional(i.e, decimal rounding or inf value handling.), so it seems that additional functions will need to be supplemented in the future.

Example

## before
print('{:5n}'.format(1234567.1))
>>> Traceback (most recent call last):
    File "<wasm>", line 1, in <module>
    ValueError: Format code 'n' for object of type 'float' not implemented yet

## after
>>> 1.23457e+06
a = '{:.8n}'.format(12345.12345)

type(a)
>>> <class 'str'>

print(a)
>>> 12345.123

Related Issue

#1656

Copy link
Copy Markdown
Contributor

@fanninpm fanninpm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few nitpicks regarding the usage of match expressions vs. if expressions. If you decide to apply these suggestions, please remember to run them through rustfmt.

Comment thread common/src/format.rs Outdated
Comment thread common/src/format.rs Outdated
Comment thread common/src/format.rs Outdated
@notJoon
Copy link
Copy Markdown
Contributor Author

notJoon commented Jan 21, 2023

@fanninpm Thanks for your advice! It's definitely better than before 👍👍

Comment thread common/src/format.rs Outdated
@youknowone
Copy link
Copy Markdown
Member

The newly added test seems fail in CPython 3.10

Traceback (most recent call last):
  File "/home/runner/work/RustPython/RustPython/extra_tests/snippets/builtin_str.py", line 619, in <module>
    assert '{:5n}'.format(1e+3) == " 1000"
AssertionError

Copy link
Copy Markdown
Member

@youknowone youknowone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test.test_format.FormatTest is now unexpected success because you correctly fixed it!
We now can remove @unittest.expectedFailure from the test.

@notJoon
Copy link
Copy Markdown
Contributor Author

notJoon commented Jan 22, 2023

@youknowone I don't know if this is right way, but I removed @unittest.expectedFailure from the test.test_format.FormatTest.

@DimitrisJim
Copy link
Copy Markdown
Member

DimitrisJim commented Jan 22, 2023

@notJoon it's the right way! You should also remove the # TODO comments before the decorators though since they aren't needed anymore.

Clippy seems to be complaining about the usage of the WhiteSpace variant, there should be a call here where we supply WhiteSpace as the padding though (or else there wouldn't be much point to an enum)

Comment thread common/src/format.rs Outdated
@notJoon notJoon requested a review from youknowone January 30, 2023 09:38
Copy link
Copy Markdown
Member

@youknowone youknowone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@youknowone youknowone added C-compat A discrepancy between RustPython and CPython A-stdlib labels Feb 22, 2023
Comment thread common/src/format.rs

fn get_separator_interval(&self) -> usize {
match self.format_type {
Some(FormatType::Number) => 9999,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this meaning something or just intended a big number? I think bigint can have more digits than 9999.

@youknowone
Copy link
Copy Markdown
Member

I thought this is done except for test decorators but seems still on working, right?

Comment thread common/src/format.rs
inter: i32,
sep: char,
disp_digit_cnt: i32,
padding: PaddingStyle,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't need this padding. It only allows whitespace or 0, but whitespace padding doesn't add seprator.

@notJoon notJoon closed this by deleting the head repository Mar 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-stdlib C-compat A discrepancy between RustPython and CPython

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants