Dojo number rounding AND decimal places for NumberTextBox / NumberSpinner - dojo

I would like to have a NumberSpinner and/or NumberTextBox be able to do two things:
Display a value with a precision of 1 decimal place.
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.

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.

Related

Set precision of shown attribute in autocad dynamic block

I made a blog that shows it current with and height in the displayed thext.
Now they have 4 decimal places (for example 5.600), see picture below.
I want them displayed with only 1 decimal place, how can i do this?
(example below would become 5.6x16.5 in stead of 5.6000x16.5000)
You can either modify the Precision property of the Field formatting so as to round the result to 1 decimal place:
Or, if you were looking to retain the precision, you could instead suppress trailing zeroes within the Additional Format options:
Alternatively, assuming that your formatting is set to Current units, you could change the value of your LUPREC system variable.

typo3 fluid format number without trailing zeros

In my typo3 extension I got a double value saved via backend. I want to put this value out in the frontend.
I want the double to be formated to use the german ',' as decimal seperator (and a max precision of 1). So I used f:format.number viewhelper to set this. However that fluid helper ist setting fixed decimals. I want dynamic decimals, as in I do not want any trailing 0s. 4.0 ought to be shown as 4.
If I do not use the viewhelper (thus outputting the raw data) the behaviour is as desired. 4.0 shows as 4, however it also does show 4.5 as 4.5 not as 4,5.
Is there a way to 'unfix' the decimals in the viewhelper?
I also tried to wrap my output in if conditions to check via modula, but this never returns a positive.
How do I output formated doubles on a precision without trailing zeros?
For just one digit (like in your example) the following code worked for me. For a double, you have to nest another if-condition in the else case since there is no else-if in fluid.
<f:if condition="{number} == {number -> f:format.number(decimals: 0)}">
<f:then>
<f:format.number decimals="0" decimalSeparator=",">{number}</f:format.number>
</f:then>
<f:else>
<f:format.number decimals="1" decimalSeparator=",">{number}</f:format.number>
</f:else>
</f:if>

wxStaticText inconsistently displays 'degree' character

In the same application I have two different instances of wxStaticText. Each displays an angular value expressed in degrees. I've tested both instances for font name and font encoding. They are the same for both. I've tested that both strings passed to SetLabel() are using the same character value, decimal 176. Yet one displays the 'degree' character (small circle, up high) as expected and the other instead displays an odd character I'm not familiar with. How can this be? Is there some other property of wxStaticText I need to test?
I can't explain what you're seeing because obviously two identical controls must behave in the same way, but I can tell you that using decimal 176 is not a good way to encode the degree sign, unless you explicitly use wxConvISO8859_1 to create the corresponding wxString.
It is better to use wxString::FromUTF8("\xc2\xb0") instead or, preferably, make sure that your source files are UTF-8 encoded and just use wxString::FromUTF8("°").
Arghhhh! Found it. I was assuming SetLabel() was wxStaticText::SetLabel(), inherited from the wxWindow base class. It's not. We have a wrapper class of our own around wxStaticText that I was not aware of. It's the wrapper class that is bollixing the string value.
Moral: When debugging unfamiliar code, don't make assumptions, step ALL THE WAY in.

What does %2.6f do in Objective C String Formatting?

Based on Apple's documentation here: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Strings/Articles/FormatStrings.html
It's pretty easy to understand the number to the right of the decimal point is the number of digis will be rounded up...
For example, %1.2f, 123456.123456 will turn out 123456.12 and %1.4f will turn out 123456.1234...
But it looks like the number to the left of decimal does nothing.
I tried changing the number to whatever I can think of, nothing happened.
What does it do?
The number before the decimal point in the format is called the format string's width. That is, if the resultant string would involves less characters than its width, it will be left-padded with blank spaces. You don't see any change because you either aren't using a high enough number (try something ridiculous like 100 or 200), or don't have a means of properly seeing your whitespace.

How to round up and return an integer value?

I know about the Math.Round and Math.Ceiling methods, but they return a Double and a Decimal. Does VB.NET have any built-in functions which always round a floating point number up, not down, with a return type of Integer? I know there's CInt, but this can round down if it's below 6.5.
From the comments I understood you wanted to round 6.1 to 7.
Just add 1 and truncate.
If it looks awkward, create a method for it.
Correction:
Unless the number already is truncated.
Addendum:
Note here that doing == with floats is not without problem; you should always have some sort of precision when trying == with floats.
Now when you have decided on this precision - then you can rewrite your code to add 0.999999 (according to precision) and the first add-0.9999-and-truncat works.
Note again that adding 0.9999 is does not really mean you add 0.9999 with our "normal" float.
So if you really want to add 0.9999 you have to work with BND and/or some monetary arithmetics.
Which you "always" should do when calculating money (or any exact decimal stuff)