Concatenates int

simendsjo via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 10 05:37:17 PDT 2014


On 07/10/2014 02:22 PM, Rikki Cattermole wrote:
> On 11/07/2014 12:11 a.m., Sean Campbell wrote:
>> i have the ints 4, 7, 0 and 1 how can i Concatenate them into four
>> thousand seven hundred and one.
> 
> If we talking at compile time definition:
> 
> int myint = 4_7_0_1;
> 
> Would work.
> However I'll assume its at runtime you really want this.
> 
> I.e. converting a string to an integer.
> 
> int myint = to!int("4" ~ "7" ~ "0" ~ "1");
> 
> Now they are not strings, and the positions of 10^ doesn't change then:
> 
> int myint = (1000 * 4) + (100 * 7) + 1;

D also has the pow operator, so you can write this as:

    int i = 4*10^^3 + 7*10^^2 + 0*10^^1 + 1*10^^0;



More information about the Digitalmars-d-learn mailing list