Ask Your Question
5

Decimal places in slider

asked 2011-11-19 17:54:50 +0100

sagefan gravatar image

updated 2011-11-19 17:56:00 +0100

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?

edit retag flag offensive close merge delete

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 ( 2011-11-19 20:36:04 +0100 )edit

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 ( 2011-11-20 03:29:30 +0100 )edit

2 Answers

Sort by » oldest newest most voted
3

answered 2011-11-19 18:30:46 +0100

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.

edit flag offensive delete link more

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 ( 2011-11-19 20:53:23 +0100 )edit
1

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

benjaminfjones gravatar imagebenjaminfjones ( 2011-11-19 21:36:11 +0100 )edit
3

answered 2013-02-21 08:16:58 +0100

Marcin Kostur gravatar image

updated 2013-02-21 08:17:41 +0100

@interact
def _(x=slider( map(lambda x:x.n(digits=4),srange( 1,10,.1)) )):
    print x
edit flag offensive delete link more

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 ( 2016-12-13 03:00:28 +0100 )edit

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: 2011-11-19 17:54:50 +0100

Seen: 1,616 times

Last updated: Feb 21 '13