-
Notifications
You must be signed in to change notification settings - Fork 25k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(core): Fix decimal pipe floating point formatting bug #53730
base: main
Are you sure you want to change the base?
fix(core): Fix decimal pipe floating point formatting bug #53730
Conversation
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
75c4a1c
to
b978193
Compare
b978193
to
9d5d49b
Compare
// e.g. '123456789.123456789' | ||
if (typeof value === 'string' && value.includes('.')) { | ||
const parts = value.split('.'); | ||
if (parts.length != 2) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The condition doesn't cover every non valid string, we should likely rely on strToNumber
to throw.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (typeof value === 'string' && value.includes('.') && strToNumber(value)) {
const parts = value.split('.');
const integerPart = parts[0];
const fractionPart = `0.${parts[1]}`;
..
Hmm you mean calling strToNumber(value)
inside of if(..)
?
Or can you give some example code? 🙇
Cause parts
is array of string, we can't pass it to strToNumber(string)
directly 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can either call strToNumber()
or probably better, extract the portion we'd like to reuse in a function :
function isStringNumber(value: number|string): boolean {
return typeof value === 'string' && !isNaN(Number(value) - parseFloat(value))
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aha~ I understood. isStringNumber
looks much better! thanks for nice suggestion 👍
edc5ec6
to
940e8d5
Compare
fix(core): Fix decimal pipe floating point formatting bug Address `JeanMeche`'s comments Extract duplicated codes to isStringNumber()
940e8d5
to
92d36d0
Compare
I need this feature and ready to update PR, so waiting your awesome reviews~ thanks! 😃 |
PR Type
What is the current behavior?
angular/packages/common/src/pipes/number_pipe.ts
Lines 104 to 105 in d315e2c
decimal_pipe
parse string to number first and then apply formating'123456789.123456789'
string to number, some digit on fraction part is lost due to javascript floating point limitationWhat is the new behavior?
Does this PR introduce a breaking change?