How to Humanize Numerical Input|Output

user1234 user1234 at 12.de
Mon Sep 9 20:43:06 UTC 2024


On Monday, 9 September 2024 at 20:02:48 UTC, Jabari Zakiya wrote:
> I have this code to input integer values:
>
> ```
>   ulong[] x;
>   foreach (_; 0 .. 2) { ulong a; readf!" %d"(a); x ~= a; }
>   end_num = max(x[0], 3);
>   start_num = max(x[1], 3);
>   if (start_num > end_num) swap(start_num, end_num);
>   start_num = start_num | 1;             // if start_num even 
> add 1
>   end_num = (end_num - 1) | 1;           // if end_num even 
> subtract 1
>   if (end_num - start_num < 2) { start_num = 7; end_num = 7; }
>
> ```
>
> Currently I have to enter data as:   123456 789102
>
> I'd like to enter data as:  123_456 789_10
>
> How do I do that?
>
> Also for output, I do this:
>
> ```
> writeln("total twins = ", twinscnt, "; last twin = ", last_twin 
> - 1, "+/-1");
> ```
>
> I'd also like to output data as:  123,987 or 123_987

About the input, see 
https://issues.dlang.org/show_bug.cgi?id=20568.
A remedy is to use a `myInputText.filter!(a => a != 
'_').to!int()` etc.

```d
import std;

void main(string[] args)
{
     // string[] inputs = args[1..$]; // let's not use stdin for 
now
     string[] inputs = ["123_456", "789_10"];
     auto filtered   = inputs.map!(i => i.filter!(n => n != '_'));
     auto values     = filtered.map!(f => f.to!int());
     writeln(values.array);
}
```

About the output I see someone just post the solution ;)


More information about the Digitalmars-d-learn mailing list