EN · DE · RU · FR · ES

#1379: NumberFormatter.kt

projectforge-business/src/main/kotlin/org/projectforge/framework/utils/NumberFormatter.kt Type: Kotlin · Role: Utility · Source: projectforge-business/src/main/kotlin/org/projectforge/framework/utils/NumberFormatter.kt 174 lines · 94 code · 69 comments · 11 blank
Static utility methods for NumberFormatter. Collection of pure functions with no side effects, providing common helper operations.

Code Structure

Annotations: param, JvmOverloads, JvmStatic, return, see

Objects: NumberFormatter, is, is, is

Functions (9): format, format, format, format, format, formatCurrency, formatNull, internalFormat, formatPercent

Properties (5): df, useLocale, amount, format, bigDecimal

Imports: 10 packages

Package: org.projectforge.framework.utils

Source Code (abridged)

package org.projectforge.framework.utils

import org.projectforge.Constants
import org.projectforge.framework.persistence.user.api.ThreadLocalUserContext
import java.math.BigDecimal
import java.math.BigInteger
import java.math.RoundingMode
import java.text.DecimalFormat
import java.text.NumberFormat
import java.time.Month
import java.time.format.TextStyle
import java.util.*

object NumberFormatter {
  /**
   * @param value Format this value.
   * @param pattern The format string for [DecimalFormat].
   * @param roundingMode [RoundingMode.HALF_UP] is default.
   * @return The formatted number or empty string if value is null.
   * @see ThreadLocalUserContext.getLocale
   */
  @JvmStatic
  @JvmOverloads
  fun format(value: Number?, pattern: String, roundingMode: RoundingMode = RoundingMode.HALF_UP): String {
    value ?: return ""
    return format(value, pattern, ThreadLocalUserContext.locale!!, roundingMode)
  }

  /**
   * @param value Format this value.
   * @param pattern The format string for [DecimalFormat].
   * @param locale The locale to use.
   * @param roundingMode [RoundingMode.HALF_UP] is default.
   * @return The formatted number or empty string if value is null.
   */
  @JvmStatic
  @JvmOverloads
  fun format(
    value: Number?,
    pattern: String,
    locale: Locale,
    roundingMode: RoundingMode = RoundingMode.HALF_UP
  ): String {
    value ?: return ""
    val df = DecimalFormat.getInstance(locale) as DecimalFormat
    df.applyPattern(pattern);
    df.roundingMode = roundingMode
    return df.format(value)
  }

  /**
   * Returns the given integer value as String representation.
   *
   * @param value The integer value to convert.
   * @param scale Scaling the output, null is default, meaning scale of object is used.
   * @return The String representation or empty/null String, if value is null.
   */
  @JvmStatic
  fun format(value: Number?, locale: Locale): String {
    return internalFormat(value, locale = locale) ?: ""
  }

  /**
   * Returns the given integer value as String representation.
   *
   * @param value The integer value to convert.
   * @param scale Scaling the output, null is default, meaning scale of object is used.
   * @return The String representation or empty/null String, if value is null.
   */
  @JvmStatic
  @JvmOverloads
  fun format(value: Number?, scale: Int? = null): String {
    return internalFormat(value, scale) ?: ""
  }

  @JvmStatic
  @JvmOverloads
  fun format(month: Month, locale: Locale? = ThreadLocalUserContext.locale): String {
    val useLocale = locale ?: Locale.ENGLISH
    return month.getDisplayName(TextStyle.FULL_STANDALONE, useLocale)
  }

  /**
   * Returns the given integer value as String representation (scale = 2).
   *
   * @param value The integer value to convert.
   */
  @JvmStatic
  @JvmOverloads
  fun formatCurrency(value: Number?, withCurrencySymbol: Boolean = false): String {
    val amount = internalFormat(value, 2) ?: return ""
    return if (!withCurrencySymbol || Constants.CURRENCY_SYMBOL.isNullOrBlank()) {
      amount
    } else {
      "$amount ${Constants.CURRENCY_SYMBOL}"
    }
  }

  /**
   * Returns the given integer value as String representation.
   *
   * @param value The integer value to convert.
   * @param defaultValue For null values this value will be returned (default is "").
   * @param scale Scaling the output, null is default, meaning scale of object is used.
   * @return The String representation or empty/null String, if value is null.
   */
  @JvmStatic
  @JvmOverloads
  fun formatNull(value: Number?, scale: Int? = null): String? {
    return internalFormat(value, scale)
  }

  private fun internalFormat(
    value: Number?,
    scale: Int? = null,
    locale: Locale = ThreadLocalUserContext.locale!!
  ): String? {
    if (value == null)
      return null
// ... (truncated, total 152 lines)

Git History

868d6abb7 2025 -> 2026
63081666f Source file headers: 2024-> 2025.
b6092df09 Copyright 2023 -> 2024
ab45d51fa Copyright 2001-2022 -> 2001-2023.
dcfc7c1a9 Old calendar moved to sub menu, ScriptingTask*, JiraUtils