Showing float numbers Grails in gsp - grails

I want to manipulate float numbers in gsp, here is what i want:
If the number has a 1.* i want it to show the dot, but if it ends with zero i dont want it to show the dot and zero.
like this:
Score: 1.5
Score: 1
Score: 2.1
Score: 3
The score variable is a float number and it is an input field on the gsp that loads the number and it can be changed.
But the real problem is, how can i see if the number has decimals?

There is already a taglib for formating numbers: (g:formatNumber)
I think something like this should work:
<g:formatNumber number="${score}" type="number" format="###.##"/>
But...if that doesn't work...
I would say write your own custom taglib. If it is something that is going to be used multiple times, why loop through a list of objects in your controller, change the float to a string just to display it? Let the page decide how to show it in the proper context.
Or
Add a transient field to the domain object (String scoreDisplay) and then have getScoreDisplay() return the value of score as a string, formatted how you want.

Well, I'd suggest you to format the number in your controller - before it gets to your gsp. That way you have more control over the number format. Once you're in the gsp, you have to use a grails' decimal number format or make your own taglib to format (since the number of fractional digits changes in your case).

Related

How do you convert text like `'$71.4145` into a number you can do arithmetic with

The title says it all. One thing I want to avoid is long formulas. If it's more than a single function, something is clearly wrong since this should be a common use case.
I've tried TO_PURE_NUMBER and VALUE
Your question suggests that the value $71.4145 is not a number but a text string. That can happen if your spreadsheet locale is such that it expects comma as decimal mark, or expects a different currency symbol. It will also happen if you have formatted the value as plain text rather than currency.
To convert the text string $71.4145 into the number 71.4145 (seventy-one and change), use regexextract(), like this:
=iferror( value( regexextract( to_text(A2), "[\d.]+" ) ) )
Just use -- to suppress text to equivalent number values. Try-
=--A1
try:
=SUBSTITUTE(A1; "$"; )*1

How can I FIND the last instance of a character in a string? (Google Sheets)

I need a Google Sheet function that will return the position of the last instance of a particular character. Basically, FIND, but starting on the right.
For example, for the data set below, I need to return the position of the last dash.
ABC-DEF-GHI = 8
ABCD-EF-GH-IJK = 11
AB-C-DE-FGH-I-JK = 14
Thanks!
I don't know where to start. MID might work, but the file names are of different lengths and different formats. The files just generally end with - ***.png, and I need the asterisk. The string I need is also of variable length and can contain spaces (the string is the name of the student).
Here's a possible solution:
=len(regexextract(A1,".*-"))
It's essentially extracting everything up to the last dash and taking the length of the resulting string.
for the whole array try:
=INDEX(LEN(REGEXEXTRACT(A1:A3; "(.*-)")))

What is the nPrecision parameter of RobotC displayLCDNumber?

Vex ROBOTC has the following command for displaying integers to the LCD display:
displayLCDNumber(nLine, nPos, nValue, nPrecision);
I'm trying to display the following: displayLCDNumber(0, 4, (float) 3.14159, nPrecision);
Supposedly, using the nPrecision parameter I should be able to display floats. Unfortunately, I cannot seem to get this to work. When I specify a negative integer, it just adds leading 0s to the displayed numbers. When I specify positive, it just shifts the integer portion of my number that many places right. When I try decimal nPrecisons such as 0.6, it does nothing. What is the nPrecision parameter and what do I need to set it too to correctly display my number?
API here: http://www.robotc.net/wikiarchive/VEX2_Functions_Display
Edit:
Any help would be apreciated, preferably as soon as possible because I will only get a chance to test/use it tomorrow and friday for like 30 minutes.

Split function treating dashes oddly

Why does =SPLIT("1,2-5,4", ",")
equal
1 42040 4
instead of
1 2-5 4 ?
I have all of the cells formatted at plain text.
Regextract should give you the desired output. Try:
=ArrayFormula(regexextract("1,2-5,4", {"^(\d+),",",(.+),",",(\d+)$"}))
To complement JPV's answer.
You can use:
=REGEXEXTRACT(A1,"(.*?),(.*?),(.*)")
which is "hard-coded" to splitting exactly 3 elements (as JPV's is). To give more flexibility, you can use something like:
=REGEXEXTRACT(A1&REPT(",",10),REPT("(.*?),",10))
which is limited to a maximum of 10 elements (that number can be changed to suit). However, it will output an array that is always that maximum number of elements long (padded out with blank cells). You could use QUERY or FILTER to filter out those blank cells - the formula will become a little convoluted.
Alternatively, you can "code" your string such that automatic date coercion is avoided, and then "uncode" it after the SPLIT:
=ArrayFormula(SUBSTITUTE(SPLIT(SUBSTITUTE(A1,"-","x"),","),"x","-"))
With 1,2-5,4 in a cell this can be split as required with Data > Split text into columns... .

integer digit grouping in grails scaffolded views

I have a default scaffolded view and in the list view, all my integer values are shown with digit grouping. (like 5,129)
I couldn't find a way to disable this behavior. I tried on both English and Turkish locale. I am not sure if it is used but the messages.properties file is all default values:
default.date.format=yyyy-MM-dd HH:mm:ss z
default.number.format=0
If I change the view and add g:formatNumber around the value such as
<g:formatNumber number="${fieldValue(bean: tagInstance, field: 'tag_id')}" type="number" groupingUsed="false" />
all that gets printed is 5 (for the number 5129)
Eliminating the fieldValue() call fixed it.
${tagInstance.tag_id}
prints the number without any formatting.
Have you tried this:
default.number.format=#
Note that the # will cause a value of 0 to not display. If you want it to display, you'll probably want:
default.number.format=#0

Resources