First time here? Check out the FAQ!

Ask Your Question
5

Decimal places in slider

asked 13 years ago

sagefan gravatar image

updated 13 years ago

Suppose you have a slider with given stepsize, say 0.1 like this

@interact
def _(a=slider(1,100,0.1)):
    print a

Is it possible to adjust this, such that the number printed to the right of the slider has only one decimal place?

Preview: (hide)

Comments

Would that just print the number with one decimal place, even though the actual number is not that? Or do you want the actual number to be what is displayed?

Jason Grout gravatar imageJason Grout ( 13 years ago )

If I set the steps to 0.03 I want 2 decimal places and so on. However it would be sufficient to adjust this independently of the step-size. Just say "I want n-decimal places"...

sagefan gravatar imagesagefan ( 13 years ago )

2 Answers

Sort by » oldest newest most voted
3

answered 13 years ago

benjaminfjones gravatar image

As it is currently implemented, I don't believe slider has such an option to change the format of the displayed value. It would be a pretty easy project for a beginner to add this functionality to Sage. I could imagine changing the code in interact.py so that the display_value option could be passed a format like "%.1f" to indicate to display 1 decimal point.

I think that would be a valuable contribution, I would certainly use it in my interact demos.

Preview: (hide)
link

Comments

I agree. It was the case a while ago that we didn't display extra (zero) digits, then it wasn't, and I think finally it's what we have now. Someone asked about that in a talk I gave *today* about making interacts! File a bug and we have fodder for SD35.5 :)

kcrisman gravatar imagekcrisman ( 13 years ago )
1

This is now Trac #12060 and I will add it to the SD 35.5 wishlist.

benjaminfjones gravatar imagebenjaminfjones ( 13 years ago )
3

answered 12 years ago

Marcin Kostur gravatar image

updated 12 years ago

@interact
def _(x=slider( map(lambda x:x.n(digits=4),srange( 1,10,.1)) )):
    print x
Preview: (hide)
link

Comments

Be aware when using this solution that digits sets the precision of the numerical value of x. Subsequent computations are limited by that precision and this can lead to unexpected round-off errors.

paulmasson gravatar imagepaulmasson ( 8 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 13 years ago

Seen: 1,739 times

Last updated: Feb 21 '13