Class Formats

java.lang.Object
io.keikai.axyra.sheets.format.Formats

public final class Formats extends Object
Number and date formatting with Excel format codes, and classification of typed input. These are the engine's formatting rules as stateless helpers; no workbook is needed. Date serials use the 1900 date system. For the text a cell shows in a workbook, which follows that workbook's date system, use Cell.formattedText().

 String s = Formats.format(1234.5, "#,##0.00");      // "1,234.50"
 ParsedValue v = Formats.parse("50%");                // NUMBER, 0.5
 
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    format(double value, String code)
    Formats a number with an Excel format code under en-US conventions, e.g.
    static String
    format(double value, String code, String localeTag)
    Formats a number with an Excel format code under a BCP-47 display locale.
    parse(String text)
    Classifies user-typed cell text the way Excel does when a value is entered into a cell: numbers (including "50%", "$1,234.56", "1/2", "1.5E3"), dates and times (as Excel serials), booleans, formulas (leading =) and plain text.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • format

      public static String format(double value, String code)
      Formats a number with an Excel format code under en-US conventions, e.g. format(1234.5, "#,##0.00") gives "1,234.50" and format(45000, "yyyy-mm-dd") gives "2023-03-15". Date serials use the 1900 date system.
      Parameters:
      value - the number or date serial
      code - the Excel format code, e.g. "0.00%"
      Returns:
      the formatted text
      Throws:
      AxyraException - if code is null
    • format

      public static String format(double value, String code, String localeTag)
      Formats a number with an Excel format code under a BCP-47 display locale. Month and weekday names, AM/PM markers, and the decimal and thousands separators follow the locale, e.g. format(1234.56, "#,##0.00", "de-DE") gives "1.234,56". The format code itself does not change with the locale: . and , in the code always mean the decimal point and the thousands separator. Date serials use the 1900 date system.
      Parameters:
      value - the number or date serial
      code - the Excel format code in en-US syntax
      localeTag - a BCP-47 tag such as "de-DE"; null, empty or unknown tags fall back to en-US
      Returns:
      the formatted text
      Throws:
      AxyraException - if code is null
    • parse

      public static ParsedValue parse(String text)
      Classifies user-typed cell text the way Excel does when a value is entered into a cell: numbers (including "50%", "$1,234.56", "1/2", "1.5E3"), dates and times (as Excel serials), booleans, formulas (leading =) and plain text. Classification uses en-US conventions and the 1900 date system. Leading and trailing spaces are ignored; whitespace-only input is TEXT with empty text. Reference: ECMA-376 Part 1 §18.17.2.3 (data types).
      Parameters:
      text - the typed text
      Returns:
      the classified value
      Throws:
      AxyraException - if text is null