aws-appsyncvtlaws-appsync-resolver

Floats rounded down in AWS AppSync VTL


I am creating a resolvers response mapping in AWS AppSync which is meant to perform a math calculation and return a percentage value as a float:

#set( $result = $ctx.source.total * 100 / 365000 )
$result

However VTL rounds this down each time to the nearest whole number such as 1.0, 2.0 etc.

Given 5000 * 100 / 365000:

Expected - 1.36 Result - 1.0

Is there anyway I can achieve this? or do I need to look towards using a Lambda (which feels overkill for something so simple).


Solution

  • The problem is that $ctx.source.total, 100, and 365000 are all treated as integers (java.lang.Integer) by VTL.

    VTL does support floating point numbers via java.lang.Double. The VTL syntax for a double instead of an integer is just 100.0 instead of 100.

    If any of the numbers in the assignment are doubles,$result will be a double.

    So you could do this and get 1.36986301369863:

    #set( $result = $ctx.source.total * 100.0 / 365000 )
    

    Here's a link to some examples and example output:

    https://mappingtool.dev/app/appsync/3d44255e560fa075b45bfa08afbb6fe4