Why does "%d" in str$ print zero?

Recurrent H/W and software problems
Post Reply
lyizb
Posts: 312
Joined: Fri Feb 12, 2021 8:23 pm
Has thanked: 163 times
Been thanked: 117 times

Why does "%d" in str$ print zero?

Post by lyizb »

wlog int(1234.5),str$(int(1234.5),"%5d"),str$(int(1234.5),"%5u")

Yields: 1234 0 0

How do I print an integer right-justified within a fixed number of characters?

(If it makes a difference, this is with a C3SuperMini.)

Also, the spacing in the documentation seems to imply that wlog "|"+str$(15,"%4.3f")+"|" would be printed with two spaces before the 15.000, but I get |15.000|.
bugs
Posts: 195
Joined: Mon Feb 08, 2021 10:10 pm
Location: Scotland
Has thanked: 92 times
Been thanked: 77 times

Re: Why does "%d" in str$ print zero?

Post by bugs »

I think you need the additional ",!" for integers...

Code: [Local Link Removed for Guests]

wlog int(12345.6),str$(int(12345.6),"%5d",1),str$(int(1234.5),"%5u",1)
lyizb
Posts: 312
Joined: Fri Feb 12, 2021 8:23 pm
Has thanked: 163 times
Been thanked: 117 times

Re: Why does "%d" in str$ print zero?

Post by lyizb »

[Local Link Removed for Guests] wrote: [Local Link Removed for Guests]Sat Jun 28, 2025 5:00 pmI think you need the additional ",!" for integers...
That works, thanks, but I can't say I understand why it should be necessary, since "%d" and "%u" imply integers, and the int() should be converting the numbers to integers.
User avatar
cicciocb
Site Admin
Posts: 3254
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 650 times
Been thanked: 2290 times
Contact:

Re: Why does "%d" in str$ print zero?

Post by cicciocb »

[Local Link Removed for Guests] wrote: [Local Link Removed for Guests]Sat Jun 28, 2025 11:26 pm
[Local Link Removed for Guests] wrote: [Local Link Removed for Guests]Sat Jun 28, 2025 5:00 pmI think you need the additional ",!" for integers...
That works, thanks, but I can't say I understand why it should be necessary, since "%d" and "%u" imply integers, and the int() should be converting the numbers to integers.
This is required because the internal printf() C-function need to know the format of the value to be converted (in annex all the variables are floating point)
Post Reply