onChange Javascript to round this.value - onchange

I've got the following onChange code. It updates my div1 with the value. The value is a numeric value and I would like to round it so that there is no decimal point. Can anyone tell me how this can be done?
So for example value 12.987654321 will be 13
onChange="document.getElementById('div1').innerHTML=this.value"

There is a Math.round function you could use like this (otherwise your code looks fine to me):
onChange="document.getElementById('div1').innerHTML=Math.round(+this.value)"

Related

Formatting column in pandas to decimal places using table.style based on value

I am trying to format a column in a dataframe using style.
So far I successfully used the styling for a fixed number of decimals:
mytable.style.format('{:,.2f}', pd.IndexSlice[:, ['Price']])
but I need to expand this to formatting based on value as this:
if value is >=1000, then format to zero decimal places
if value is between 1000 and 1, then format to two decimal places
if value is < 1, then format to five decimal places
Does anyone have a solution for this?
Thank you!
Building upon #Code_beginner's answer – the callable should return formatted string as output:
def my_format(val):
if val >= 1000:
return f"{val:,.0f}"
if val >= 1:
return f"{val:,.2f}"
return f"{val:,.5f}"
mytable.style.format({'Price': my_format})
What you are looking for is called "conditional formatting". In which you set the conditions like you described and return the right format. There are examples in the documentation, they used a lambda function there. But you can also create a normal function which might look something like this:
def customfunc(val):
if val>=1000:
format='{:,.0f}'
if val<1000 and val>=1:
format='{:,.2f}'
if val<1:
format='{:,.5f}'
return format
df.style.format({0:customfunc})
This should style your first column like described in your problem. If the columns has a name you have to adjust it accordingly. If you have trouble see the documentation linked abve there are more examples.
Just to have it visually clear, this is how it looks now:
That's my line of code:
df.style.format({'Price': customfunc})

dojo/mvc/at doesn't return dijit/form/DateTextBox in format of constraints datePattern

This seem's to be a question often asked, but there doesn't seem to be an easy answer or an answer at all, so I risk a duplicate here, and ask again - I feel like having a puzzle with 4 pieces and don't manage to put them together:
I'm using a dojo date picker like this
<input data-dojo-type="dijit/form/DateTextBox"
data-dojo-props="constraints: { datePattern: 'yyyy-MM-dd'},
value: at(model, 'myDate')" />
The date picker displays the date in UI like I want, but the value that's assigned in model.myDate keeps being in ISO format - I'd need that to be in yyyy-MM-dd, too.
I know that I can use dojo.date.locale.format to post-process the value, but that would be after it is saved in model.myDate. I'd like to return the value in the correct format right away. Return value null if there's no input, return value undefined if there's no valid value, and return value in format yyyy-MM-dd when the given date is valid.
Maybe I can integrate that call to dojo.date.locale.format somehow? Something like .transform(..) or whatever is possible in dojo!?
I also read about overwriting the serialize method, but I don't see how and where to do that in here.
Any ideas or hint in the right direction? Many thanks in advance.
Hi just wondering if something like at(model, prop).transform(converterObj) helps: http://dojotoolkit.org/reference-guide/1.10/dojox/mvc/at.html#data-converter

VueJS int Props changing value on prefix with 0

I am trying to pass through an int value as a prop.
So if I call:
<job-cards-create :jobno="1203"></job-cards-create>
I get:
But if I add even one '0' infront:
<job-cards-create :jobno="01203"></job-cards-create>
It gives:
What is going on? Am I missing something?
This is because your number 01203 is being interpreted as an octal number due to the leading zero. Check out these examples:
01203 === 643 // true
01203.toString() // "643"
Here is some documentation on octals in JS
Do you really need it to be a number? It doesn't look like you're going to be performing any calculations on it. Just knock off the v-bind and use a string literal instead:
<job-cards-create jobno="01203"></job-cards-create>

Access SQL INT function

I'm trying to extract some numbers with decimals but need to remove the decimal part. There's no fixed length on any of both sides.
I have already tried:
INT()
INTR()
ROUND()
Usually INT() should solve this but sometimes it doesn't return the correct number (for example, INT(3) returns 2).
Did you try the function TRUNC(number, [, decimal_places]) ?
EDIT
For the round precision you can see this link
I ended up solving it on my own! ROUND didn't work and FLOOR was also undefined...
I ended up needing to use two INTs with a FORMAT in one of them.
Thank you anyway.

Dojo number rounding AND decimal places for NumberTextBox / NumberSpinner

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.