Insert a char in string

simendsjo via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 10 13:44:36 PDT 2014


On 07/10/2014 09:58 PM, Alexandre wrote:
> basically format....
> I read a cobol struct file...
> 
> From pos X to Y I have a money value... but, this value don't have any
> format..
> 
> 000000000041415
> 
> The 15 is the cents... bascally I need to put the ( comma ), we use
> comma to separate the cents, here in Brazil...
> 
> On Thursday, 10 July 2014 at 19:33:15 UTC, simendsjo wrote:
>> On 07/10/2014 06:05 PM, Alexandre wrote:
>>> I have a string X and I need to insert a char in that string...
>>>
>>> auto X = "100000000000000";
>>>
>>> And I need to inser a ',' in position 3 of this string..., I try to use
>>> the array.insertInPlace, but, not work...
>>>
>>> I try this:
>>> auto X = "100000000000000";
>>> auto N = X.insertInPlace(1,'0');
>>
>> Do you really want to insert a comma in the string, or do you want to
>> format a number as "100,000,000,000.00"?
> 

I'm not sure what you're trying to do though.
Do you need to fix the file by adding a comma at appropriate places? Or
read it into D and write it to the console with your currency format?
This is one way of reading in the values using slices and std.conv:

import std.stdio, std.conv;
void main() {
    immutable input = "000000000041415";
    double amount = input[0..$-2].to!double();
    amount += input[$-2..$].to!double() / 100;
    writeln(amount);
}



More information about the Digitalmars-d-learn mailing list