Right-align formatted numbers

New to FreeBASIC? Post your questions here.
Post Reply
Flyzone
Posts: 109
Joined: Nov 17, 2017 17:39

Right-align formatted numbers

Post by Flyzone »

Is there a quick way to right align formatted numbers so they are columnwise aligned on a printout?

This sample program left-aligns them. I need them to be right-aligned like in a spreadsheet. Hopefully this is a simple specification rather than string conversions with length and space padding calculations.

Code: Select all

#lang "fblite"
#include "vbcompat.bi"

for i=1 to 10000
    print format(i,"###,##0")
next i
sleep
Last edited by Flyzone on Jan 11, 2023 23:42, edited 1 time in total.
fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Right-align formatted numbers

Post by fxm »

Aligning by 'FORMAT' does not work.
Use 'PRINT USING' instead:

Code: Select all

#lang "fblite"

for i=1 to 10000
    print using "###,###"; i
next i
sleep
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Right-align formatted numbers

Post by dodicat »

You also have rset, lset,
(See the help manual)

Code: Select all

dim as string s=space(30)
for n as long=1 to 20
   rset s,str(500*(rnd-rnd))
   print s
next
sleep
 
Flyzone
Posts: 109
Joined: Nov 17, 2017 17:39

Re: Right-align formatted numbers

Post by Flyzone »

Thanks folks.

I just assumed USING would do the same thing without trying it. Duh.

I should have said "decimal aligned, right justified" so the RSET version achieves what I asked for but not what I meant though I think

Code: Select all

rset s,str(format(500*(rnd-rnd),"###,##0.00"))

would accomplish that.

Thanks for the RSET since I had not used that before.

:mrgreen:
Post Reply