I have a very weird scenario in struts2.
When I do the following:
<s:property value="%{4/2}"/>
I get 2.
But when I do the following:
<s:property value="%{2/4}"/>
I get a big fat 0.
the property tag is always outputting an Integer even when the evaluated value is a double. How can I change this?
You're doing integer arithmetic because your inputs are integers, not doubles: two divided by four is zero for integers. Try this:
<s:property value="%{2.0/4.0}"/>
instead.
Related
If I have a tag like the below and the value test comes from a database and is an integer but th:text converts it to a floating point number e.g.....
<p th:text="${test}"/>
Where test....
{
"test": 5
}
the result is 5.0
However annoyingly if I use a number literal in thymeleaf e.g.
<p th:text="5+0"/>
the result is displayed correctly as 5
I know I can use something like <p th:text="${#numbers.formatDecimal(test,0,0)}"/> however this is problematic as I don't know if the value could be a number or not until runtime and so it will clutter it up with if statements etc. Would be so much better if it finds an integer to actually keep it as an integer (or convert it to a zero decimal text) - e.g. like it does with number literal. Is there any way to do this?
Many thanks for any help
In an edit.gsp, where I have inputfields for decimal number, the decimal number show up as '3.123' and if I save it: I got error as the decimal point is wrong! It expect a ','. My locale is Sweden.
So I have to manually replace dot's with comma for all decimal numbers.
I've looked around the whole week and could not find a solution anywhere.
Shouldn't Grails be consistent and show commas when it expect commas in the save?
It should work with both BigDecimal and for double.
I have Grails 3.2.4
Here is an example of a "g:field" from an edit-form:
Bredd: <g:field type="number decimal" name="width" min="20" max="300" required="Y" value="${request1?.width}" style="width: 4em"/>
So, what can I do?
manually replacing dots sounds all horrible and possibly the wrong approach.
Moved answer segments around
So an update on the answer since i hit a similar issue today maybe in reverse. Sent through a number of 3333 when validation failed for another reason the number in the field had become 3,333 after the validation failure. If the old validation issue is fixed it will now fail due to comma in the number.
The reason turned out to be :
<g:textField value="${fieldValue(bean: instance, field: 'someField')}"
upon return changed number to 3,333 when changing this to
value="${instance.someField}"
Above was actual issue #larand was facing
I would store the input field width as a short
so :
Class MyClass {
//if you are storing a number like 123 (non decimal)
Short width
//if you are storing 12.12 which becomes 1212 when stored
Integer width
BigDecimal getDecimalWidth() {
return new BigDecimal(this.width).movePointRight(2).setScale(2)
}
void setWidth(BigDecimal decimal) {
this.width=new BigDecimal(decimal).movePointLeft(2).setScale(2)
}
//unsure but think this should work
Integer getWidthInteger() {
return this.width as int
}
void setWidth(Integer integer) {
this.width=(byte)integer
}
}
This will then give you methods to get the short value as big decimal using ${instance.decimalWidth} or as integer : ${instance.widthInteger}
when your field is actually numeric:
<g:formatNumber number="${myCurrencyAmount}" type="currency" currencyCode="EUR" />
To me that seems a lot more straight forward and cleaner than chopping up numbers which well you think about it
After first validation issue the number was 3333 as put in. So maybe this is your issue ? Unsure since you are talking of dots
Within a view I have the following code:
#foreach (var ce in Model.MyObjectCollection)
{
<p>
Target: #ce.Target.ToString("#.##")%
<br/>
Level: #ce.Level.ToString("#.##")%
</p>
}
If either the value of Target or Level is 0.00, then MVC simply renders that out as an empty string. Debugging shows the value of each to be 0.00.
I need zero values to render as 0.00%.
Can anyone advise if I'm doing something wrong?
The # format specifier won't display zeros
Note that this specifier never displays a zero that is not a significant digit, even if zero is the only digit in the string. It will display zero only if it is a significant digit in the number that is being displayed.
You can use the 0 specifier instead
ToString("0.00")%
Use zeros instead of #:
#ce.Target.ToString("0.00")%
I have EditField. I want to show virtual keyboard only with number, without letters. Is it possible?
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
You can also use
object.setFilter(TextFilter.get(TextFilter.NUMERIC));
amount->setInputMode(bb::cascades::TextFieldInputMode::NumbersAndPunctuation);
You can find all the types here:
https://developer.blackberry.com/cascades/reference/bb__cascades__textfieldinputmode.html
You can either construct the EditField and pass EditField.FILTER_NUMERIC as the style: http://www.blackberry.com/developers/docs/7.0.0api/net/rim/device/api/ui/component/BasicEditField.html#FILTER_NUMERIC
OR
You can call EditField.setFilter() http://www.blackberry.com/developers/docs/7.0.0api/net/rim/device/api/ui/component/BasicEditField.html#setFilter(net.rim.device.api.ui.text.TextFilter) and pass in aTextFilter. Use the static TextFilter.get() function and pass in one of the filter constants from the TextFilter class, eg http://www.blackberry.com/developers/docs/7.0.0api/net/rim/device/api/ui/text/TextFilter.html#NUMERIC
Numeric filters accept non-negative integers only (0 - 999999999 and beyond), Integer filter accepts all integers negative, zero and positive, and REAL_NUMERIC accepts any decimal number (negative, zero, positive, with an optional decimal point).
Currently i have a field that takes in a telephone number.
It performs a length validation without fail. What i really want is there to be length validation only when a value is input, and no validation when there is no value. How can i accomplish this?
<p:inputText id="corporateTel" label="#{labelResource.telephone}">
<f:validateLength minimum="5" maximum="20" />
</p:inputText>
This is already the default behaviour. Your problem is caused elsewhere. Perhaps you used an int instead of Integer or String, so that it defaults to 0 instead of null. Or perhaps you used an Integer while running the webapp on Tomcat/JBoss wherein the default Apache EL implementation would implicitly coerce it to 0 instead of null. You can turn it off by adding the following VM argument:
-Dorg.apache.el.COERCE_TO_ZERO=false
Or use String instead of Integer. This is also a more appropriate data type for phone numbers as they may have a leading 0 which would otherwise be chopped off when using Integer.