Author Topic: numberformat question  (Read 1375 times)

0 Members and 2 Guests are viewing this topic.

jcfuller

  • Guest
numberformat question
« on: July 31, 2019, 06:19:13 AM »
Charles,
  I want to print a column of numbers lined up by the decimal point. I tried a couple variations of numberformat but can't seem to find it there.
Code: [Select]
   .111
  6.400
 13.300
179.212
James

Charles Pegge

  • Guest
Re: numberformat question
« Reply #1 on: July 31, 2019, 10:56:55 AM »
I've created 2 functions to aid clean column alignments:

Code: [Select]
function padstr(int lmar,wid,rmar,string d) as string
=====================================================
  string s=string(wid," ")
  mid(s,1,d) 'd will be right clipped
  function=string(lmar," ")+s+string(rmar," ")
end function

function numstr(int wid,rmar,dp, double v) as string
====================================================
  string s,ng
  int n,ls
  if v<0
    v=-v
    ng="-"
  endif
  n=10^dp
  s=str(round(v*n))
  ls=len(s)
  if ls<=dp
    s=string(dp+1-ls,"0")+s
  endif
  ls=len(s)
  s=ng+left(s,ls-dp)+"."+mid(s,-dp)
  ls=len(s)
  if ls>wid 'ensure number is never clipped
    wid=ls
  endif
  function=string(wid+rmar," ")
  mid(function,wid-ls+1,s)
end function

included in jjArray:
Code: [Select]
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*

$filename "t.exe"
use rtl64
use console
use corewin
% ITEMS = 10

include "getfields.o2bas"
'==============================================================================
Macro LineInput string (s,c,b=1024)
s = nuls b
s = (char*)fgets(s,b,c)
End Macro
'==============================================================================
Macro Pause
Print cr "Press any key to continue" cr
waitkey
End Macro
'==============================================================================
Type InfoType
char Country[64]
float f1990
float f2013
End Type
'==============================================================================
'32bit cdecl callback
'Function TypeCompare cdecl (void *a, void *b) As Integer, callback

'64bit callback
Function TypeCompare (void *a, void *b) As Integer, callback
InfoType Ptr ia = (InfoType Ptr) a
InfoType Ptr ib = (InfoType Ptr) b
Function = ((100 * ia.f2013) - (100 * ib.f2013))
End Function


function padstr(int lmar,wid,rmar,string d) as string
=====================================================
  string s=string(wid," ")
  mid(s,1,d) 'd will be right clipped
  function=string(lmar," ")+s+string(rmar," ")
end function

function numstr(int wid,rmar,dp, double v) as string
====================================================
  string s,ng
  int n,ls
  if v<0
    v=-v
    ng="-"
  endif
  n=10^dp
  s=str(round(v*n))
  ls=len(s)
  if ls<=dp
    s=string(dp+1-ls,"0")+s
  endif
  ls=len(s)
  s=ng+left(s,ls-dp)+"."+mid(s,-dp)
  ls=len(s)
  if ls>wid 'ensure number is never clipped
    wid=ls
  endif
  function=string(wid+rmar," ")
  mid(function,wid-ls+1,s)
end function

 

Function main() As sys
string sFileIn = "UnderFiveMortalityRate.tab"
string sLine
sys fp1,fields,count
'InfoType Info[25]

Dim InfoType Info at getmemory sizeof(InfoType)*25

fp1 = fopen sfileIn,"r"
If not fp1 Then
printl "PROBLEM #1"
Pause
exit function
End If
count = 0
'sLine = LineInput(fp1)
Do
sLine = LineInput(fp1)
If Len(sLine) = 0 Then
Exit Do
End If
fields = GetFields(sLine,chr(9))
'print  "fields = " fields cr
If (Len(arg(3)) = 0) OR (Len(arg(4)) = 0) Then
continue Do
End If

Info[count].Country = arg(1)
Info[count].f1990 = val(arg(3))
Info[count].f2013 = val(arg(4))
count++
loop


fclose(fp1)
'fails on qsort this call
qsort(Info,count,sizeof(Info),@TypeCompare)

int i
string s1
double f1,f2
for i=1 to count
  s1=Info[i].Country
  f1=Info[i].f1990
  f2=Info[i].f2013
  print padstr(2,12,1,s1)+numstr(8,1,2,f1)+numstr(8,1,2,f2) cr
next

freememory @Info
Print "end" cr
wait
Function = 0
End Function

return main()

jcfuller

  • Guest
Re: numberformat question
« Reply #2 on: July 31, 2019, 01:13:20 PM »
Charles,
  Excellent .. but I'm confused with the last 2 lines of  numstr function??

James

Charles Pegge

  • Guest
Re: numberformat question
« Reply #3 on: July 31, 2019, 01:28:41 PM »

function=string(wid+rmar," ")
allocation of spaces to return.

mid(function,wid-ls+1,s)
patch the formatted number into the space
same as: mid$(function,wid+ls+1)=s 

jcfuller

  • Guest
Re: numberformat question
« Reply #4 on: July 31, 2019, 01:30:58 PM »
Charles,
  Thank you. I got it. I'm used to Function = to be the same as a return and code is not executed after it.
I also looked up mid and understand now
James

jcfuller

  • Guest
Re: numberformat question
« Reply #5 on: July 31, 2019, 02:17:33 PM »
Charles,
  Do I assume correctly the formatting functions will find their way into one of the inc files - StringUtil?
I tried putting them at the end of the source with a #lookahead at the top but it failed.

James


Brian Alvarez

  • Guest
Re: numberformat question
« Reply #6 on: August 01, 2019, 12:27:26 PM »
James, in the Dr Mario code i posted, there is a FORMAT function that works like the PB one. You can use it if you need to.

Charles Pegge

  • Guest
Re: numberformat question
« Reply #7 on: August 02, 2019, 07:34:01 PM »
James,

I'll include those functions in StringUtil. - but I may change the param order.

I would also recommend msvcrt sprintf, but there are problems using float variadics in  64bit, which I have to fix for the next release of o2.
« Last Edit: August 02, 2019, 07:49:23 PM by Charles Pegge »