Ask Your Question

# Decimal places in slider

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

( 2011-11-19 20:36:04 +0200 )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"...

( 2011-11-20 03:29:30 +0200 )edit

## 2 Answers

Sort by » oldest newest most voted
@interact
def _(x=slider( map(lambda x:x.n(digits=4),srange( 1,10,.1)) )):
print x

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.

( 2016-12-13 03:00:28 +0200 )edit

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.

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

( 2011-11-19 20:53:23 +0200 )edit
1

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

( 2011-11-19 21:36:11 +0200 )edit

## Your Answer

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

Add Answer

## Stats

Asked: 2011-11-19 17:54:50 +0200

Seen: 1,342 times

Last updated: Feb 21 '13