# String formatting? (ex. ints, space-padded, width = 3)

**URL:** <https://community.haxe.org/t/string-formatting-ex-ints-space-padded-width-3/2229>\
**Category:** Haxe\
**Created:** [January 25, 2020, 7:24am UTC](https://community.haxe.org/t/string-formatting-ex-ints-space-padded-width-3/2229 "2020-01-25T07:24:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![uvtc](https://community.haxe.org/user_avatar/community.haxe.org/uvtc/32/1198_2.png) [@uvtc](https://community.haxe.org/u/uvtc)\
**Post date:** [January 25, 2020, 7:24am UTC](https://community.haxe.org/t/string-formatting-ex-ints-space-padded-width-3/2229/1 "2020-01-25T07:24:33Z")

</div>

I’d like to create strings that include ints and display them like:

```haxe
  0 -- 5
  5 -- 10
 10 -- 15
 ...
 95 -- 100

```

Given integers like those, how can I specify string formatting like “integers, take up 3 places, right-justified, padded with spaces”?

---

<div class="post-metadata">

**Author:** ![vlado](https://community.haxe.org/user_avatar/community.haxe.org/vlado/32/1074_2.png) [@vlado](https://community.haxe.org/u/vlado)\
**Post date:** [January 25, 2020, 9:52am UTC](https://community.haxe.org/t/string-formatting-ex-ints-space-padded-width-3/2229/2 "2020-01-25T09:52:42Z")

</div>

[https://api.haxe.org/StringTools.html](https://api.haxe.org/StringTools.html)

StringTools.lpad for your particular example.

---

<div class="post-metadata">

**Author:** ![Gama11](https://community.haxe.org/user_avatar/community.haxe.org/gama11/32/7_2.png) [@Gama11](https://community.haxe.org/u/Gama11)\
**Post date:** [January 25, 2020, 9:52am UTC](https://community.haxe.org/t/string-formatting-ex-ints-space-padded-width-3/2229/3 "2020-01-25T09:52:44Z")

</div>

`StringTools.lpad()` would do the trick:

```haxe
using StringTools;

class Main {
	static function main() {
		for (_ in 0...10) {
			var a = Std.string(Math.floor(Math.random() * 500)).lpad(" ", 3);
			var b = Std.string(Math.floor(Math.random() * 500)).lpad(" ", 3);
			trace('$a -- $b');
		}
	}
}

```

```haxe
source/Main.hx:8: 157 -- 35
source/Main.hx:8: 369 -- 392
source/Main.hx:8: 324 -- 460
source/Main.hx:8: 96 -- 2
source/Main.hx:8: 165 -- 49
source/Main.hx:8: 373 -- 184
source/Main.hx:8: 367 -- 375
source/Main.hx:8: 262 -- 24
source/Main.hx:8: 218 -- 421
source/Main.hx:8: 436 -- 408

```

---

<div class="post-metadata">

**Author:** ![RealyUniqueName](https://community.haxe.org/user_avatar/community.haxe.org/realyuniquename/32/10_2.png) [@RealyUniqueName](https://community.haxe.org/u/RealyUniqueName)\
**Post date:** [January 25, 2020, 11:50am UTC](https://community.haxe.org/t/string-formatting-ex-ints-space-padded-width-3/2229/4 "2020-01-25T11:50:42Z")

</div>

Offtopic: `Math.floor(Math.random() * 500)` is the same as `Std.random(500)` and `'$value'` (or `"" + value`) is usually faster than `Std.string(value)` for numeric types 😉

---

<div class="post-metadata">

**Author:** ![Gama11](https://community.haxe.org/user_avatar/community.haxe.org/gama11/32/7_2.png) [@Gama11](https://community.haxe.org/u/Gama11)\
**Post date:** [January 25, 2020, 12:03pm UTC](https://community.haxe.org/t/string-formatting-ex-ints-space-padded-width-3/2229/5 "2020-01-25T12:03:09Z")

</div>

> [@RealyUniqueName](#):
>
> and `'$value'` (or `"" + value` ) is usually faster than `Std.string(value)` for numeric types

What…

---

<div class="post-metadata">

**Author:** ![RealyUniqueName](https://community.haxe.org/user_avatar/community.haxe.org/realyuniquename/32/10_2.png) [@RealyUniqueName](https://community.haxe.org/u/RealyUniqueName)\
**Post date:** [January 25, 2020, 12:05pm UTC](https://community.haxe.org/t/string-formatting-ex-ints-space-padded-width-3/2229/6 "2020-01-25T12:05:24Z")

</div>

Because `Std.string(value)` generates a call, while `"" + value` on some targets is generated as-is and handled by the target platform.

---

<div class="post-metadata">

**Author:** ![uvtc](https://community.haxe.org/user_avatar/community.haxe.org/uvtc/32/1198_2.png) [@uvtc](https://community.haxe.org/u/uvtc)\
**Post date:** [January 25, 2020, 3:46pm UTC](https://community.haxe.org/t/string-formatting-ex-ints-space-padded-width-3/2229/7 "2020-01-25T15:46:57Z")

</div>

Cool, thanks, all!

---

<div class="post-metadata">

**Author:** ![uvtc](https://community.haxe.org/user_avatar/community.haxe.org/uvtc/32/1198_2.png) [@uvtc](https://community.haxe.org/u/uvtc)\
**Post date:** [January 25, 2020, 4:05pm UTC](https://community.haxe.org/t/string-formatting-ex-ints-space-padded-width-3/2229/8 "2020-01-25T16:05:15Z")

</div>

> [@RealyUniqueName](#):
>
> ‘$value’

Ah, nice. Thanks!

---

<div class="post-metadata">

**Author:** ![intoxopox](https://community.haxe.org/user_avatar/community.haxe.org/intoxopox/32/535_2.png) [@intoxopox](https://community.haxe.org/u/intoxopox)\
**Post date:** [February 6, 2020, 10:50pm UTC](https://community.haxe.org/t/string-formatting-ex-ints-space-padded-width-3/2229/9 "2020-02-06T22:50:54Z")

</div>

Man, I’d love to see a pinned forum post collection of x is faster than y Haxe code samples.
