Reputation: 145
Alright, so I have a working program that makes golden ratio rectangles based on a length or width given.
This probably isn't a good practice, but I wrote a String.Slice(start, end) in the extensions class.
Here's what I need help with.
case 1: //Length
l1.Text = value.ToString().Slice(0, 4);
l2.Text = value.ToString().Slice(0, 4);
h1.Text = (value/phi).ToString().Slice(0, 4);
h2.Text = (value/phi).ToString().Slice(0, 4);
break;
case 2: //Width
l1.Text = (value * phi).ToString().Slice(0, 4);
l2.Text = (value * phi).ToString().Slice(0, 4);
h1.Text = value.ToString().Slice(0, 4);
h2.Text = value.ToString().Slice(0, 4);
break;
Depending on the radiobutton, it finds the length or with based on what you provide. The problem is that with the strings all sliced to 1-4 characters, a number can be shown as
"161."
(with the period) in the textbox. Is there a way to make it so that only if it ends with a period that the period is removed? Thanks.
P.S. Here's the slice function for reference:
public static class Extensions
{
public static string Slice(this string source, int start, int end)
{
if (end < 0) // Keep this for negative end support
{
end = source.Length + end;
}
int len = end - start; // Calculate length
try
{
return source.Substring(start, len); // Return Substring of length
}
catch (Exception)
{
try
{
return source.Substring(start, len - 1); // Return Substring of length
}
catch (Exception)
{
try
{
return source.Substring(start, len - 2); // Return Substring of length
}
catch (Exception)
{
return source.Substring(start, len - 3); // Return Substring of length
}
}
}
}
}
Upvotes: 0
Views: 170
Reputation: 3283
Why are you using this strange Slice() function? Wouldn't it be enough to use String.Format with a special format string? For example instead of writing
h1.Text = (value/phi).ToString().Slice(0, 4);
just write:
h1.Text = String.Format("{0:0.0}", value/phi);
...and so on?
Upvotes: 4