That removes the white space from both sides of the string.
Note: your original won’t work becuase result hasn’t been assigned anything so there’s nothing to replace. You need to trim/replace on i, the input to the function.
You are inside a JavaScript function. You only have JavaScript stuff available to you unless you import it. DecimalFormat is Java, not JavaScript.
Also, result is a String, not a number so even if DecimalFormat were available without importing it, it wouldn’t work because format() requires a number, not a String.
So you can either do this using just JavaScript stuff:
Or you can use the Java class but you’ll have to import it.
(function(i) {
var DecimalFormat = Java.type('java.text.DecimalFormat');
return new DecimalFormat('#.##').format(parseFloat(i.trim()));
})(input)
Notice you still need to trim i and parse it into a float.
IIRC errors thrown in transformations generate more meaningful errors in OH 4 now. If using a GraalVM JS SCRIPT transform, you have access to the full helper library meaning it’s easy to add logging to transformations. I believe jRuby and Rules DSL has the same. It’s only the old Nashorn JS (i.e. the JS Transformation instead of the SCRIPT transformation) where logging and other stuff is a pain.