Right justification of numbers

Just starting out? Need help? Post your questions and find answers here.
Jeff88
New User
New User
Posts: 8
Joined: Thu Jan 03, 2019 3:05 am

Right justification of numbers

Post by Jeff88 »

I am new to PB so perhaps there is a better way to do this. Also this is such a minor bit of code compared to so much else I have observed in this forum.

I wanted to display the numbers in a matrix in straight right-justified columns using proportional fonts. The standard Windows fixed fonts were ugly for this in my opinion. The problem of course is the width of a space is less than a diget. My solution was to use the space characters available with unicode. Chr($2007) is the same space as 0-9 and Chr($2004) is the same space as a minus sign. Not sure if this is true for all fonts and sizes. Here is a snippet of code I used to do this.

I am using standard Windows Arial fonts. It turned out that my Win7 desktop had an old version of the font that did not have the extra characters. I just copied the Ariel font from my Win10 laptop which took care of this. The second code listing is a simple program to display the width of a font. Hope this is useful to some of you.

Code: Select all

        ns1$=Chr($2007)
        ListIconGadget(40,left,40,700,500,"Col/Row",60)
        SetGadgetColor(40,#PB_Gadget_BackColor,$FDF0E5)
        For i=1 To ncmps
          AddGadgetColumn(40,i,RSet(Str(i),4,ns1$),50)
        Next
        SetGadgetFont(40,FontID(Font4))
        For i=1 To ncmps                         ;For each row of B-Matrix  (note B is a square matrix)
          p$=RSet(Str(i),6,ns1$)
           For k=1 To ncmps                      ;For each col of B-Matrix
             If Bmatrix(i,k)<0.0                  ;use slightly different spacing due to minus sign
              p$=p$+#CRLF$+RSet(StrF(Bmatrix(i,k)*10000.0,3),6,ns1$)
            Else
              p$=p$+#CRLF$+Chr($2004)+RSet(StrF(Bmatrix(i,k)*10000.0,3),5,ns1$)
            EndIf
          Next
          AddGadgetItem(40,-1,p$) 
          If Mod(i,5)=0                                ;highlight every fifth line for clarity
            SetGadgetItemColor(40,i-1,#PB_Gadget_BackColor,$DDFDFD,#PB_All)
          EndIf
        Next
Font width lister. Note many fonts do not have the hex 2000 to 200B characters, often will print as a square.

Code: Select all

If OpenWindow(0, 0, 0, 1000,500,"", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  font1= LoadFont(#PB_Any, "Arial MS Unicode",18)
  StartDrawing(WindowOutput(0))
  DrawingFont(FontID(font1))
  For i=32 To 127                                                                       ;normal ASCII characters
    Debug Str(i)+"  " +Chr(i)+ "  " +StrF(TextWidth(Chr(i)))
  Next
  For i=$2000 To $200B                                                              ;special unicode spaces
    Debug Hex(i)+"  " +Chr(i)+ "  " +StrF(TextWidth(Chr(i)))
  Next
  StopDrawing()  
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
End
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Right justification of numbers

Post by skywalk »

Use fixed pitch font like consolas or courier.
Use tabs.
Use Lset, Rset functions.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Post Reply