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")%
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
I have the following Thymeleaf code, I want "--" to be printed if taxHistory.landValue equals to zero, and the actual value to be printed if taxHistory.landValue is greater than 0
<td th:if"${taxHistory.landValue==0}" th:text="--"></td>
<td th:unless="${taxHistory.landValue>0}" th:text="|${taxHistory.landValue}|"></td>
However I'm getting the following error
org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "0}"
What is the correct way of doing this in Thymeleaf?
There are multiple ways to do this, but you can use a ternary operator and print your string if your desired condition does not match:
<td th:text="${taxHistory.landValue > 0 ? taxHistory.landValue : '--' }">[taxHistory.landValue]</td>
You can use the > notation to keep the HTML formed properly. This assumes that a negative value would also print your string (you didn't mention behavior in that unusual case).
How can I test if a certain character of a string variable is a digit in SPSS (and then apply some operations, depending on the result)?
So let's for example say, I have a variable that reflects the street number. Some street numbers have additional character at the end e.g. "12b". Now let's further assume that I extracted the last character (that could be a digit, or the additional letter) into a string variable. After that I'd like to check if this character is a digit or a letter. How can this be done?
I managed to do this with the MAX function, where "mychar" is the character variable to be checked:
COMPUTE digitcheck = (MAX(mychar,"9")="9").
If the content of "mychar" is a digit [0-9] the result of the MAX function will be "9" otherwise the MAX function will return the letter and the equality test fails.
In this way you can also check if a whole string variable contains a letter or not. It looks pretty ugly though, because you have to compare every single character of your string variable.
compute justdigits = (MAX((CHAR.SUBSTR(mystr,1,1), CHAR.SUBSTR(mystr,2,1), CHAR.SUBSTR(mystr,3,1), ..., CHAR.SUBSTR(mystr,n,1),"9")="9").
If you try to turn a letter into a number then it becomes a missing value. Therefore, to test whether a character is a digit, you can do this:
if not missing(number(YourCharacter,f1)) .....
The same test can determine whether a string has only a number in it or not:
compute OnlyNumber=(not missing(number(YourString,f10))).
Note: using the number command on strings will produce warning messages which you can of course ignore.
I have a code, which shows price:
def value
h.number_with_precision(object.value, precision: 2,strip_insignificant_zeros: false)
end
And i put this value in haml: %b= #{number.value}
For example: showing price "1234.02" inside <b/> and I need some styles to ".02". How to put precision inside span?
EDIT:
Ruby string have formatter built in. No need to use any class.
Just use "%.2f" % #{number.value}
You have to split number.value with '.' as separator, and use it like
full_value = number.value.to_s.split('.')
haml: %b #{full_value[0]}.
%span #{full_value[1]}
As you are not stripping insignificant zeroes you will always have 3 digits at the end you need to format (assuming you're including the decimal place).
You can do
number.value.to_s[0..-4]
Which will give you all characters in the string apart from the last 3, and then
number.value.to_s.last(3)
To return the part you want to format separately
If you don't want to format the decimal point, and just need the last 2 characters separated:
number.value.to_s[0..-3]
number.value.to_s.last(2)
You can then format these as needed
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