Ask Your Question

Decimal places in slider

asked 2011-11-19 10:54:50 -0500

sagefan gravatar image

updated 2011-11-19 10:56:00 -0500

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

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


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 13:36:04 -0500 )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-19 20:29:30 -0500 )edit

2 answers

Sort by » oldest newest most voted

answered 2011-11-19 11:30:46 -0500

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 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


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 13:53:23 -0500 )edit

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

benjaminfjones gravatar imagebenjaminfjones ( 2011-11-19 14:36:11 -0500 )edit

answered 2013-02-21 01:16:58 -0500

Marcin Kostur gravatar image

updated 2013-02-21 01:17:41 -0500

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


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-12 20:00:28 -0500 )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


Asked: 2011-11-19 10:54:50 -0500

Seen: 413 times

Last updated: Feb 21 '13