Negative Numbers and NumberFormat

Bob Sneidar bobsneidar at iotecdigital.com
Thu Apr 6 12:15:08 EDT 2017


I already have some functions for formatting numerical values I can submit for review. formatDate() is already in the masterLibrary.

function formatMoney theValue
   -- strip out monetary symbols
   replace "$" with empty in theValue
   replace "¢" with empty in theValue

   switch
      case theValue = 0 or theValue >= 1
         put format("$%1.2f", theValue) into theValue
         break
      case theValue >= 0
         put format("%#.2f¢", theValue) into theValue
         break
      case theValue <= -1
         replace "-" WITH empty in theValue
         put format("($%1.2f)", theValue) into theValue
         break
      case theValue < 0
         replace "-" WITH empty in theValue
         put format("(%#.2f¢)", theValue) into theValue
         break
   end switch

   return theValue
end formatMoney


function formatPhone thePhoneNumber, theFormat
   -- text
   -- mid
   if theFormat is empty then
      put "classic" into theFormat
   end if

   if thePhoneNumber is empty then
      return thePhoneNumber
   end if

   put word 2 to -1 of thePhoneNumber into theExtension
   delete word 2 to -1 of thePhoneNumber

   repeat WITH i = 1 to the number of chars of thePhoneNumber
      if char i of thePhoneNumber is a number then
         put char i of thePhoneNumber after theNewValue
      end if
   end repeat

   put the length of theNewValue into theLength
   if theLength is not among the items of "7,10" then
      return thePhoneNumber && theExtension
      exit formatPhone
   end if

   put "-" before char -4 of theNewValue

   if theLength is 10 then
      if theFormat is "classic" then
         put ") " after char 3 of theNewValue
         put "(" before theNewValue
      else
         put "-" after char 3 of theNewValue
      end if
   end if

   if theExtension is not empty then put " " & theExtension after theNewValue
   return theNewValue
end formatPhone



On Apr 6, 2017, at 08:35 , Richard Gaskin via use-livecode <use-livecode at lists.runrev.com<mailto:use-livecode at lists.runrev.com>> wrote:

Perhaps this raises a more interesting question:

Should we consider creating a new more fully-featured function which not only behaves as we might find ideal for decimal values, but also supports formatting of phone numbers, currency, and other common display formats?

Maybe a pair of functions would be useful so we could easily transform between display and calculation formats, something like:

put formattedValue(".25", "currency-US") -- returns: $0.25
put rawValue("$0.25". "currency-US")     -- returns: 0.25

Should there be some provision for doing that on a list where a column specifier could be supplied?:

put rawValue(tSomeLlist, "currency-US", 2) -- returns the list with
   values in column 2 converted to calculable formats.

This could be written in LC Script or LC Builder - might be a good community project....

--
Richard Gaskin



More information about the use-livecode mailing list