I am trying to do math on statement amounts - but ...
# general
k
I am trying to do math on statement amounts - but it's treating it like a string. I've tried parseFloat() and number() to get it to not treat it as text but then the form is erroring out. Is there an easier way to do this?
Copy code
<#assign openiningbal = 0>
	<#assign totnewchg = 0>
	<#assign totpaycr = 0>
	<#assign closingbal = 0>

	<#list statement.lines as line>
		<#if line_index==0>
			<#assign openiningbal = line.balance>	
		</#if>
		<#if line?is_last> 
			<#assign closingbal = line.balance>	
		</#if>
		<#assign totnewchg = totnewchg + (line.charge) >	
		<#assign totpaycr = line.payment + totpaycr>	
	</#list>
the (line.charge) seems to be what is stored as text. Just trying to calculate a "total new charges" and a "total credits"
a
FreeMarker has builtins that convert text type values to number type values: https://freemarker.apache.org/docs/ref_builtins_string.html#ref_builtin_number It's a good idea to print a small debug value to make sure it is in fact a string: https://freemarker.apache.org/docs/ref_builtins_expert.html#ref_builtin_isType If you get a "Yes" from that builtin, then the ?number builtin is probably the right too for the job. If it's actually not a string but a different type, look up the builtins for that type instead. Using builtins to cast the data types of all the variables involved in the arithmetic to match each other (where necessary), you can get the expected calculation to go through.
All that being said, it may in some cases be wiser to keep as much of the logic out of the template as possible, by doing the calculation on a custom field instead, either defined via SuiteCloud customization or created ad-hoc via beforeLoad script on the print event. It depends on the needs of your project.
k
So I was able to determine that it is a string. I can convert it to having no dollar symbol, no commas, and using ?number results in an error >.<
a
What error?
k
Just the general unhelpful "an error has occured"
I did figure out - it's because of null values
and I've got the math working now.=
<#assign totnewchg = totnewchg?number + line.charge!"0"?replace("$","")?replace(",","")?number >
of course, now it's just an ugly no currency symbol, no commas number. but the math is working!
a
Oh yeah! Gotta check for those before involving them in arithmetic, or use that null default thing (!) There are regex for numeric vs non-numeric digits, and there are freemarker builtins to render a number as current locale's currency format. You're on your way!