Skip to content
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(slider): round decimals in the thumb label #2527

Merged
merged 5 commits into from
Jan 30, 2017

Conversation

crisbeto
Copy link
Member

@crisbeto crisbeto commented Jan 4, 2017

Rounds down the thumb label value to a maximum of one decimal place, in order to avoid displaying weird rounding errors.

Fixes #2511.

@crisbeto crisbeto requested a review from jelbourn January 4, 2017 21:22
@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Jan 4, 2017
/** The value to be used for display purposes. */
get displayValue(): string|number {
// Skip adding the decimal part if the number is whole.
return this.value % 1 === 0 ? this.value : this.value.toFixed(1);
Copy link
Member

Choose a reason for hiding this comment

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

Using one decimal place is too prescriptive, since having a step of, say 0.05 between 0 and 1 is valid. It would be nice to have the slider intelligently figure out the appropriate number of decimal places to show based on the step value.

Copy link
Member Author

Choose a reason for hiding this comment

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

That's what I was thinking initially, however anything longer than 3 characters doesn't look right with the current setup.

Copy link
Contributor

Choose a reason for hiding this comment

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

I would round it based on the step and consider the 3 characters thing a separate bug (we'll still have issues with 1000 even with no decimals)

Copy link
Member Author

Choose a reason for hiding this comment

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

Alright, I'll redo it.

@jelbourn jelbourn requested a review from mmalerba January 4, 2017 21:29
@jelbourn
Copy link
Member

jelbourn commented Jan 4, 2017

cc @mmalerba

@crisbeto
Copy link
Member Author

@mmalerba I've switched it over to determine the rounding based on the step.

/** The value to be used for display purposes. */
get displayValue(): string|number {
if (this._roundLabelTo && this.value % 1 !== 0) {
return this.value.toFixed(this._roundLabelTo);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think there's a couple cases this misses:
.999 to 2 places
.899 to 2 places
.001 to 2 places

Copy link
Member Author

Choose a reason for hiding this comment

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

I wanted to keep this as simple as possible, because it gets called on every change detection.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, well at least leave a note so that if this issue pops up we know that it was an intentional choice

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

@@ -226,6 +235,18 @@ export class MdSlider implements ControlValueAccessor {
set vertical(value: any) { this._vertical = coerceBooleanProperty(value); }
private _vertical = false;

/** The value to be used for display purposes. */
get displayValue(): string|number {
// Note that this could be improved further by rounding something like 0.999 to 0.99 or
Copy link
Contributor

Choose a reason for hiding this comment

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

.999 rounds to 1, .899 rounds to .9 (when rounding to 2 decimal places)

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry, apparently I forgot how rounding works. Fixed now.

@mmalerba
Copy link
Contributor

lgtm, lint seems to be failing but i don't think its related to this PR

@mmalerba mmalerba added the action: merge The PR is ready for merge by the caretaker label Jan 24, 2017
@kara kara merged commit 987897c into angular:master Jan 30, 2017
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker cla: yes PR author has agreed to Google's Contributor License Agreement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

slider decimal point
5 participants