0

I would like to have a NumberSpinner and/or NumberTextBox be able to do two things:

  1. Display a value with a precision of 1 decimal place.
  2. Round those values to the .5

I would also like it if the rounding occured while typing or at the minimum when the textbox loses focus. If I attempt to type 5.2 it will round to 5.0. If I type 6.345 it will round to 6.5.

I've attempted a wild mixture of values using places, pattern and round properties on both the constraints and/or editOptions objects. Nothing works.

Notes:

  • As for a NumberSpinner I set smallDelta to 0.5 and it will increment property when using the spinner arrows.
  • From what I understand the places property will override any pattern property you've set.
  • I have yet to get rounding to work.
  • According to the source documentation for dojo/number I should be able to use a pattern based on these guidelines to round with. I've tried various versions of #.50## with no success. It seems to add 50 to the inputed value with no rounding. Maybe I'm not getting it?
  • I see the default editOptions pattern value is #.##### but when the textbox loses focus it rounds to three decimal places. So something else is overriding that pattern?
  • Digging through some of the Dijit code it looks like the round() method of dojo/number is not being passed the an increment parameter in NumberTextBox.js in the filter() method. So setting round as a property of one of the two config objects above does nothing at all?

Here is a basic jsFiddle using NumberSpinner that attempts some rounding and decimal precision.

One of my attempts:

<input 
  id="test" 
  type="text"
  data-dojo-type="dijit/form/NumberSpinner" 
  data-dojo-props="
    value: 5,
    smallDelta: 0.5,
    constraints: { min: 5, max: 1000, places: 1, round: 5 },
    editOptions: {
      places: 1
    }
    ">

I'm sure I can get by with extending a method on NumberTextBox or NumberSpinner like filter() via a custom mixin but would prefere to do it through config options on data-dojo-props if at all possible.

Like with a lot of Dojo somethings aren't as apparent or easy as they seem they would be, especially with Dijits. An there's just not a lot of info out there on some of the Dijits and the documentation loves to gloss over things and has always been a huge weak point of dojo.

Can anyone help clarify any of this for me. I feel like I am close and need someone to point out what I'm mostlikely overlooking. I would simply like to round a value to the nearest .5 and/or display a single decimal place that is rounded.

hungerstar
  • 21,206
  • 6
  • 50
  • 59

1 Answers1

1

I believe rounding is deprecated in dojo or at least not supported but they really do need to update their manuals. See here. I took a look into this a while back and couldn't figure out a good solution with just using constraints or aspect and rounding before/after. The big problem is that if you set "places" to be 1, then a single digit input like "7" will resolve as NaN because it does not have 1 decimal place. I ended up just extending the NumberSpinner (mainly just the "parse" function) by doing some pre-processing. I know it's not what you wanted to do but looking at the dojo tickets, I don't think there's been much movement on the whole rounding front for dojo.

Richard
  • 1,138
  • 1
  • 6
  • 11
  • Thanks for the feedback. It's good to hear from other people trying to do similar things and how they've managed things in Dojo. I was beginning to think that a custom Number Spinner was going to be in the works for myself also. – hungerstar Dec 22 '15 at 22:10