to_char_count Function

public function to_char_count(n) result(trimmed_str)

format a non-negative cell/element count with an SI suffix 500 -> “500 cells”, 40000 -> “40.0 K cells”, 4_000_000 -> “4.0 M cells”, 4_000_000_000 -> “4.0 B cells”

Arguments

Type IntentOptional Attributes Name
integer(kind=int64), intent(in) :: n

Return Value character(len=:), allocatable


Variables

Type Visibility Attributes Name Initial
character(len=64), private :: str
real(kind=dp), private :: x

Source Code

   function to_char_count(n) result(trimmed_str)
      !! format a non-negative cell/element count with an SI suffix
      !! 500 -> "500 cells", 40000 -> "40.0 K cells",
      !! 4_000_000 -> "4.0 M cells", 4_000_000_000 -> "4.0 B cells"
      integer(kind=int64), intent(in) :: n
      character(len=:), allocatable :: trimmed_str
      character(len=64) :: str
      real(kind=dp) :: x

      if (n < 1000_int64) then
         write (str, "(I0,A)") n, " cells"
      else if (n < 1000000_int64) then
         x = real(n, dp)/1.0e3_dp
         write (str, "(F0.1,A)") x, " K cells"
      else if (n < 1000000000_int64) then
         x = real(n, dp)/1.0e6_dp
         write (str, "(F0.1,A)") x, " M cells"
      else
         x = real(n, dp)/1.0e9_dp
         write (str, "(F0.1,A)") x, " B cells"
      end if
      trimmed_str = trim(str)
   end function to_char_count