Right justify in FORMAT

New to FreeBASIC? Post your questions here.
Post Reply
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Right justify in FORMAT

Post by fzabkar »

I'm trying to print a 9-digit numeric quantity in right justified format.

This produces left justified output:

Code: Select all

Format( dwLenFilA, "#########" )
This produces right justified output:

Code: Select all

Right( Space( 9 ) & Format( dwLenFilA, "#########" ), 9 )
Is there a simpler way?
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: Right justify in FORMAT

Post by fzabkar »

It seems to me that Format is a lot less useful than it could be. Leading spaces seems to be an obvious omission, but why?

Code: Select all

#include "vbcompat.bi"

Dim dwLen as ULong = 23456789

Print Format( dwLen, "#########" )
Print Format( dwLen )
Print Str( dwLen )

Print Format( dwLen, "000000000" )

Sleep
UEZ
Posts: 988
Joined: May 05, 2017 19:59
Location: Germany

Re: Right justify in FORMAT

Post by UEZ »

This works, toov (Space(digits - Len(dwLenFilA)) & dwLenFilA):

Code: Select all

#include "string.bi"

Dim As Long dwLenFilA= 123456, digits = 9

? Right( Space( digits ) & Format( dwLenFilA, "#########" ), digits )
? Space(digits - Len(Str(dwLenFilA))) & dwLenFilA
Sleep
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: Right justify in FORMAT

Post by fzabkar »

Thanks. That was one alternative I had considered.

It just seems to me that the Format options should align with the Print Using formats, otherwise it is confusing.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Right justify in FORMAT

Post by fxm »

See this old bug report:
#369 Several Print, Print Using and Format issues, part III.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Right justify in FORMAT

Post by dodicat »

Freebasic has an rset, which is simpler than format.

Code: Select all


Dim s As String= Space(9)
RSet s,str(123456)
Print s 
sleep

 
I only use format for date time stuff or scientific notation to ordinary
format(1e-8) = .00000001
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: Right justify in FORMAT

Post by fzabkar »

Assuming it is possible, could someone please show me how I could use a macro to replace ...

Code: Select all

Right( Space( 9 ) & Str( dwLenFilA ), 9 )
... with ...

Code: Select all

Strg( dwLenFilA , 9 )
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Right justify in FORMAT

Post by fxm »

#define Strg( string , size ) Right( Space( size ) & Str( string ), size )
Last edited by fxm on Jun 07, 2022 21:00, edited 1 time in total.
Reason: updated
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: Right justify in FORMAT

Post by fzabkar »

Thanks. I'm going with this:

Code: Select all

#define RJstrg( value, numdigs ) Right( Space( numdigs ) & Str( value ), numdigs )
I would have used RSET if it had been a function rather than a sub.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Right justify in FORMAT

Post by dodicat »

There are ways to do this without using the messy format.
Some more colourful than others.
E.G.

Code: Select all


#include "crt.bi"


function rightset(s as string,n as long,flag as boolean=true) as string
    dim as zstring * 100 g
    var L=len(s)
    if flag then L=0
    sprintf(g,"%"+str(n+L)+"s",s)
    return g
end function

function setright(s as string,n as long) as string
    dim as string g=space(n)
    rset g,s
    return g
    end function

dim as string s(...)={"Mess","about","with","the","C","runtime","press","a","key","to","end"}

for n as long=lbound(s) to ubound(s)
    color n+2
print rightset(s(n),20)
next
print
s(4)="FreeBASIC"
for n as long=lbound(s) to ubound(s)
    color n+2
print setright(s(n),20)
next
print
sleep




 
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Right justify in FORMAT

Post by dodicat »

Here is another simple rightset, it lets you create a table.

Code: Select all


function setrightL(s as string,n as long,flag as boolean=false) as string
    if flag=false then locate ,n-len(s)+1 else locate ,n
    return s
end function

function setrightR(s as string,n as long) as string
    dim as string g=space(n)
    rset g,s
    return g
end function

print "Using locate"
locate ,45

print chr(31)
for n as long=10 to 25
    print chr(124)+str(sqr(n));setrightL(chr(124)+" = the square root of "+str(n)+chr(124),45)
next
print:print
print "using space"
print
for n as long=10 to 25
    print chr(124)+str(sqr(n));setrightR(chr(124)+" = the square root of "+str(n)+chr(124),30)
next

sleep
 
Post Reply