Search code examples
neo4jcypherneo4j-apocapoc

Converting an integer to a fixed-length string with leading zeros


Ideally I want toFixed function in JavaScript https://www.w3schools.com/jsref/jsref_tofixed.asp I know I can do this with Cypher like below.

WITH 3 AS inputNumber
RETURN
  CASE
    WHEN inputNumber < 10 THEN '0' + toString(inputNumber)
    ELSE toString(inputNumber)
  END AS formattedString;

but a simple APOC function would be better


Solution

  • Try:

    RETURN apoc.number.format(125.4, '0000.000') as value;
    

    It returns: "0125.400"

    Or to match your example:

    RETURN apoc.number.format(3, '00') as value;
    

    returns "03"