zimmer9
asked on
How to create an ouput decimal value based on a variable length input field using C#?
I am developing a C# console application using Visual Studio 2005.
How would you modify the following code in the code section that follows
to accept an input value of a string of anywhere from 1 to 10 numeric characters and output a decimal value of up to 8 leading numbers followed by a decimal and 2 values
to the right of the decimal representing cents.
I also want to validate that the string of characters read are all numeric
and with a value for the input field greater than zero.
For example: Input = 0000000035 yields an output value of .35
Input = 0000000135 yields an output value of 1.35
How would you modify the following code in the code section that follows
to accept an input value of a string of anywhere from 1 to 10 numeric characters and output a decimal value of up to 8 leading numbers followed by a decimal and 2 values
to the right of the decimal representing cents.
I also want to validate that the string of characters read are all numeric
and with a value for the input field greater than zero.
For example: Input = 0000000035 yields an output value of .35
Input = 0000000135 yields an output value of 1.35
if (reader.Name == "csc:amount")
{
arr1[i, 9] = "GROUP_FIELD_NAME:CheckAmount";
string value = reader.ReadElementContentAsString();
if (value.Length >= 2)
{
Int64 intTest = 0;
if (Int64.TryParse(value, out intTest) && intTest > 0)
{
arr1[i, 10] = "GROUP_FIELD_VALUE:" + value.Insert(value.Length - 2, ".");
arr1[i, 10] = "GROUP_FIELD_VALUE:" + value;
}
else
{
arr1[i, 10] = "GROUP_FIELD_VALUE:" + value.Insert(value.Length - 2, ".");
sw1.WriteLine("GROUP_FIELD_NAME:ItemSequence:" + itemsequence + ",GROUP_FIELD_NAME:CheckAmount" + "," + "GROUP_FIELD_VALUE:" + value);
}
}
else
{
arr1[i, 10] = "GROUP_FIELD_VALUE:" + value;
sw1.WriteLine("GROUP_FIELD_NAME:ItemSequence:" + itemsequence + " Field CheckAmount has to be a minimum of 2 characters");
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Ha, I didn't notice you already have TryParse() in your code. ;)
That will check to make sure value is all numeric.
That will check to make sure value is all numeric.
BTW, check my code:
Open in new window
I think you may be facing problem like value of "value" is not updated after
arr1[i, 10] = "GROUP_FIELD_VALUE:" + value.Insert(value.Length - 2, ".");
because value.Insert() doesn't modify the value of "value", It returns the modified value.