Name

nfs()

Description

Utility function for formatting numbers into strings. Similar to nf() but leaves a blank space in front of positive numbers, so they align with negative numbers in spite of the minus symbol. There are two versions, one for formatting floats and one for formatting ints. The values for the digits, left, and right parameters should always be positive integers.

Examples

  • int a=200, b=-40, c=90; 
    String sa = nfs(a, 10); 
    println(sa);  // Prints " 0000000200" 
    String sb = nfs(b, 5); 
    println(sb);  // Prints "-00040" 
    String sc = nfs(c, 3); 
    println(sc);  // Prints " 090" 
     
    float d = -200.94, e = 40.2, f = -9.012; 
    String sd = nfs(d, 10, 4); 
    println(sd);  // Prints "-0000000200.9400" 
    String se = nfs(e, 5, 3); 
    println(se);  // Prints " 00040.200" 
    String sf = nfs(f, 3, 5); 
    println(sf);  // Prints "-009.01200" 
    

Syntax

  • nfs(num, digits)
  • nfs(nums, digits)
  • nfs(nums, left, right)
  • nfs(num, left, right)

Parameters

  • num(int, float)the number to format
  • digits(int)number of digits to pad with zeroes
  • nums(int[], float[])the numbers to format
  • left(int)the number of digits to the left of the decimal point
  • right(int)the number of digits to the right of the decimal point

Return

  • String or String[]
Creative Commons License

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.