Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

As I understood it, if you did something in Kotlin like String.format("%3d", 4) (arbitrary example put illustrates my point) you would get a string like " 3" (padded to the left by two spaces). For some reason, when I do that in Kotlin, it is instead centering the string (so I get something like " 3 "). Any clue why this could be happening? Everything I'm reading says right-aligned is the default, but that's not happening for me.

Example:

hp.text = String.format("%-10s%4d%-5s%4d%-5s%4d", "HP:", pokemon.hp, "EVs:", pokemon.hpEv, "IVs:", pokemon.hpIv)

For context, this info is read in from the output of a Lua script reading the memory of a Desmume emulator playing a Pokemon game. Basically this string is in 6 sections. First the name of the stat, then the stat itself. Followed by "EVs:" and the number of EVs for that stat, and "IVs:" and the number of IVs for that stat. For now, between left aligning the text (which does work for some bizarre reason) and putting tabs () between everything, I've gotten it look... acceptable. But what's happening here is that the numbers seem to be centered within their section of the string. Here's a picture of what this ends up looking like.

Dragonite Example

I would have expected the numbers to be right-aligned so that the 1s place in each of them lines up, but that's not happening for some reason.

Link to github repo

UPDATE: I've also tried left-aligning everything by adding -after all the percent signs and that makes all the numbers left-aligned with each other. It's just so baffling to me that I can't seem to get it to right-align for the life of me. Dragonite again, left-aligned this time

UPDATE 2: I figured it out, put my answer down below. A key point that I didn't realize when I posted this (I adopted this codebase, didn't create it from scratch), was that it was using tornadofx, possibly with a default non-monospaced font.

question from:https://stackoverflow.com/questions/66057180/kotlin-string-format-is-centering-my-text

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
310 views
Welcome To Ask or Share your Answers For Others

1 Answer

I figured it out!

I think the issue ended up being the default font set by tornadofx's GUI elements. I suspect the reason was a non-monospaced font causing the spaces to be smaller than the characters in such a way that they ended up looking centered. I fixed it by setting a Monospaced font in the style I was using for the text I wanted to be formatted.

font = Font.font(java.awt.Font.MONOSPACED, 10.0)

You may need to import javafx.scene.text.Font.

Here's how it looks now. Not a huge fan of the font so I might look for a better looking monospaced font, but this at least has the formatting I want.

Flaafy with properly right-aligned stats.

Will be on the lookout for any other potential better answers.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...