Skip to content

convert python % string formatting to v interpolation#41

Merged
medvednikov merged 1 commit intovlang:masterfrom
ylluminate:fix-percent-formatting
Feb 20, 2026
Merged

convert python % string formatting to v interpolation#41
medvednikov merged 1 commit intovlang:masterfrom
ylluminate:fix-percent-formatting

Conversation

@ylluminate
Copy link
Contributor

Converts Python % string formatting expressions to V string interpolation
with correct format specifiers.

Changes

  • convert_percent_format() in helpers.v: parses Python % format
    specifiers (%s, %d, %f, %e, %g, %x, %o, %%) and emits V
    interpolation with correct format specs
    • Preserves Python default precision (6 decimal places for bare %f/%e)
    • Left-align flag overrides zero-pad (%-05d -> :-5)
    • Bare dot precision means 0 (%.f -> :.0f)
  • visit_binop() in transpiler.v: detects Mod with string Constant
    on left side and routes to format conversion instead of raw %
    • Integer modulo (10 % 3) passes through correctly

Test plan

  • 109 pass, 0 fail, 0 skip (V 0.5.0 a9423b5, macOS)
  • New test case string_format_percent covers: %s, %d, %.2f, %05d,
    %x, multi-value tuple, %%, default %f, %.f, %-05d, integer modulo
  • Generated V code compiles and produces output matching Python runtime

add convert_percent_format() in helpers.v that parses Python % format
specifiers and emits V string interpolation with correct format specs.

handles %s, %d/%i, %f/%F, %e/%E, %g/%G, %x/%X, %o, and %% (literal).
preserves Python default precision (6 decimal places for bare %f/%e).
left-align flag overrides zero-pad (%-05d -> :-5, not :0-5).
bare dot precision means 0 (%.f -> :.0f, not default 6).
integer modulo (int % int) passes through correctly.

in transpiler.v, visit_binop detects Mod with string Constant on the
left side and routes to convert_percent_format instead of raw %.

109 pass, 0 fail, 0 skip (v 0.5.0 a9423b5)
@medvednikov medvednikov merged commit c6e0036 into vlang:master Feb 20, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants