This question already has answers here:
Why did I get the compile error "Use of unassigned local variable"?
(10 answers)
Closed 2 days ago.
I keep getting this error for annualRate, monthlyCharge, and lateFee.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lab_5___Danny_Curro
{
class Program
{
static void Main(string[] args)
{
string firstName;
string lastName;
int accNumber;
string creditPlan;
double balance;
string status;
Boolean late = false;
double lateFee;
double monthlyCharge;
double annualRate;
double netBalance;
Console.Write("Enter First Name: ");
firstName = Console.ReadLine();
Console.Write("Enter Last Name: ");
lastName = Console.ReadLine();
Console.Write("Enter Account Number: ");
accNumber = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter Credit Card Plan Number[Blank Will Enter Plan 0]: ");
creditPlan = Console.ReadLine();
Console.Write("Enter Balance: ");
balance = Convert.ToDouble(Console.ReadLine());
Console.Write("Is This Account Late?: ");
status = Console.ReadLine().Trim().ToLower();
if (creditPlan == "0")
{
annualRate = 0.35; //35%
lateFee = 0.0;
monthlyCharge = balance * (annualRate * (1 / 12));
return;
}
if (creditPlan == "1")
{
annualRate = 0.30; //30%
if (status == "y")
{
late = true;
}
else if (status == "n")
{
late = false;
}
if (late == true)
{
lateFee = 25.00;
}
monthlyCharge = balance * (annualRate * (1 / 12));
return;
}
if (creditPlan == "2")
{
annualRate = 0.20; //20%
if (status == "y")
{
late = true;
}
else if (status == "n")
{
late = false;
}
if (late == true)
{
lateFee = 35.00;
}
if (balance > 100)
{
monthlyCharge = balance * (annualRate * (1 / 12));
}
else
{
monthlyCharge = 0;
}
return;
}
if (creditPlan == "3")
{
annualRate = 0.15; //15%
lateFee = 0.00;
if (balance > 500)
{
monthlyCharge = (balance - 500) * (annualRate * (1 / 12));
}
else
{
monthlyCharge = 0;
}
return;
}
netBalance = balance - (lateFee + monthlyCharge);
Console.WriteLine("Name: \t\t\t {0} {1}", firstName, lastName);
Console.WriteLine("Account Number: \t{0}", accNumber);
Console.WriteLine("Credit Plane: \t\t{0}",creditPlan);
Console.WriteLine("Account Late: \t\t{0}", late);
Console.WriteLine("Balance: \t\t{0}", balance);
Console.WriteLine("Late Fee: \t\t{0}", lateFee);
Console.WriteLine("Interest Charge: \t{0}", monthlyCharge);
Console.WriteLine("Net Balance: \t\t{0}",netBalance);
Console.WriteLine("Annual Rate: \t\t{0}", annualRate);
Console.ReadKey();
}
}
}
The compiler isn't smart enough to know that at least one of your if blocks will be executed. Therefore, it doesn't see that variables like annualRate will be assigned no matter what. Here's how you can make the compiler understand:
if (creditPlan == "0")
{
// ...
}
else if (creditPlan == "1")
{
// ...
}
else if (creditPlan == "2")
{
// ...
}
else
{
// ...
}
The compiler knows that with an if/else block, one of the blocks is guaranteed to be executed, and therefore if you're assigning the variable in all of the blocks, it won't give the compiler error.
By the way, you can also use a switch statement instead of ifs to maybe make your code cleaner.
Change your declarations to this:
double lateFee = 0.0;
double monthlyCharge = 0.0;
double annualRate = 0.0;
The error is caused because there is at least one path through your code where these variables end up not getting set to anything.
Because if none of the if statements evaluate to true then the local variable will be unassigned. Throw an else statement in there and assign some values to those variables in case the if statements don't evaluate to true. Post back here if that doesn't make the error go away.
Your other option is to initialize the variables to some default value when you declare them at the beginning of your code.
Give them a default value:
double lateFee=0.0;
double monthlyCharge = 0.0;
double annualRate = 0.0;
Basically, all possible paths don't initialize these variables.
Use the keyword "default"!!!
string myString = default;
double myDouble = default;
if(!String.IsNullOrEmpty(myString))
myDouble = 1.5;
return myDouble;
There are many paths through your code whereby your variables are not initialized, which is why the compiler complains.
Specifically, you are not validating the user input for creditPlan - if the user enters a value of anything else than "0","1","2" or "3", then none of the branches indicated will be executed (and creditPlan will not be defaulted to zero as per your user prompt).
As others have mentioned, the compiler error can be avoided by either a default initialization of all derived variables before the branches are checked, OR ensuring that at least one of the branches is executed (viz, mutual exclusivity of the branches, with a fall through else statement).
I would however like to point out other potential improvements:
Validate user input before you trust it for use in your code.
Model the parameters as a whole - there are several properties and calculations applicable to each plan.
Use more appropriate types for data. e.g. CreditPlan appears to have a finite domain and is better suited to an enumeration or Dictionary than a string. Financial data and percentages should always be modelled as decimal, not double to avoid rounding issues, and 'status' appears to be a boolean.
DRY up repetitive code. The calculation, monthlyCharge = balance * annualRate * (1/12)) is common to more than one branch. For maintenance reasons, do not duplicate this code.
Possibly more advanced, but note that Functions are now first class citizens of C#, so you can assign a function or lambda as a property, field or parameter!.
e.g. here is an alternative representation of your model:
// Keep all Credit Plan parameters together in a model
public class CreditPlan
{
public Func<decimal, decimal, decimal> MonthlyCharge { get; set; }
public decimal AnnualRate { get; set; }
public Func<bool, Decimal> LateFee { get; set; }
}
// DRY up repeated calculations
static private decimal StandardMonthlyCharge(decimal balance, decimal annualRate)
{
return balance * annualRate / 12;
}
public static Dictionary<int, CreditPlan> CreditPlans = new Dictionary<int, CreditPlan>
{
{ 0, new CreditPlan
{
AnnualRate = .35M,
LateFee = _ => 0.0M,
MonthlyCharge = StandardMonthlyCharge
}
},
{ 1, new CreditPlan
{
AnnualRate = .30M,
LateFee = late => late ? 0 : 25.0M,
MonthlyCharge = StandardMonthlyCharge
}
},
{ 2, new CreditPlan
{
AnnualRate = .20M,
LateFee = late => late ? 0 : 35.0M,
MonthlyCharge = (balance, annualRate) => balance > 100
? balance * annualRate / 12
: 0
}
},
{ 3, new CreditPlan
{
AnnualRate = .15M,
LateFee = _ => 0.0M,
MonthlyCharge = (balance, annualRate) => balance > 500
? (balance - 500) * annualRate / 12
: 0
}
}
};
Your assignments are all nested within your conditional if blocks which means that there is potential for them to never be assigned.
At the top of your class, initialise them to 0 or some other value
The compiler is saying that annualRate will not have a value if the CreditPlan is not recognised.
When creating the local variables ( annualRate, monthlyCharge, and lateFee) assign a default value (0) to them.
Also, you should display an error if the credit plan is unknown.
Not all code paths set a value for lateFee. You may want to set a default value for it at the top.
You don't assign values outside of the if statements ... and it is possible that credit might be something other than 0, 1, 2, or 3, as #iomaxx noted.
Try changing the separate if statements to a single if/else if/else if/else. Or assign default values up at the top.
If you declare the variable "annualRate" like
class Program
{
**static double annualRate;**
public static void Main() {
Try it..
Related
I am having trouble with this line of code right here...why am I being prompted with this error? I am getting an error saying "Operator '|' cannot be applied to operands of type 'bool' and 'string' How do check if my residency variable is not equal to these 2 strings I have listed in the if statement?
catch (ArgumentException)
{
if (age > 16 | age > 80)
{
Console.WriteLine("You can only enter states of OH or MI ad the driver's age must be between 16 and 80.");
}
if (residency != "OH" | "MI")
{
Console.WriteLine("You can only enter states of OH or MI ad the driver's age must be between 16 and 80.");
}
}
Here is the full code if you want to get a better idea of what I am trying to execute.
class Program
{
static void Main(string[] args)
{
CarInsurance create = new CarInsurance();
Console.Write("Enter the age of the driver: ");
int age = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the state the driver lives in: ");
string residence = Convert.ToString(Console.ReadLine());
create.PremiumCalculate(age, residence);
}
}
class CarInsurance
{
public int driverAge { get; set; }
public string residency { get; set; }
public int totalPremium;
public int GetPremium()
{
return totalPremium;
}
public void PremiumCalculate(int age, string residency)
{
int premiumOhio = 100;
int premiumMichigan = 250;
try
{
if (residency == "MI")
{
int total = (100 - age) * 3 + premiumMichigan;
Console.WriteLine("Your premium is {0}", total.ToString("C"));
}
if (residency == "OH")
{
int total = (100 - age) * 3 + premiumOhio;
Console.WriteLine("Your premium is {0}", total.ToString("C"));
}
}
catch (ArgumentException)
{
if (age > 16 | age > 80)
{
Console.WriteLine("You can only enter states of OH or MI ad the driver's age must be between 16 and 80.");
}
if (residency != "OH" | "MI")
{
Console.WriteLine("You can only enter states of OH or MI ad the driver's age must be between 16 and 80.");
}
}
}
}
You should apply it between two conditions, not values. Note
if (residency != "OH" || residency != "MI")
Note, however, that this condition will always return true. You probably meant to use &&:
if (residency != "OH" && residency != "MI")
Let me explain the issue first. Here residency != "OH" | "MI", you are evaluating residency != "OH" and this works and produces bool. Then produced bool fails against | "MI" because operator | is invalid for strings.
If you had 2 strings, you would get
Operator '|' cannot be applied to operands of type 'string' and 'string'
One way to fix it is
using System.Linq;
. . ..
var exclusionList = new [] {"OH","MI"};
if (!exclusionList.Contains(residency))
How about something like this. First, I start off by listing all states (yeah, you only care about two of them, but any of them is a valid state):
public enum ResidentState
{
AL,
AK,
AZ,
AR,
CA,
CO,
CT,
DE,
FL,
GA,
HI,
ID,
IL,
IN,
IA,
KS,
KY,
LA,
ME,
MD,
MA,
MI,
MN,
MS,
MO,
MT,
NE,
NV,
NH,
NJ,
NM,
NY,
NC,
ND,
OH,
OK,
OR,
PA,
RI,
SC,
SD,
TN,
TX,
UT,
VT,
VA,
WA,
WV,
WI,
WY,
}
Then I start my car insurance class:
class CarInsurance
{
public int DriverAge { get; private set; }
public ResidentState Residency { get; private set; }
public int TotalPremium { get; private set; }
private bool _ageOk = false;
private bool _residencyOk = false;
}
It has publicly readable properties for DriverAge, ResidentState and TotalPremium. They can only be set from within the class. It also has a few bookkeeping flags for age and residency validity.
Then, I add a couple of member functions to that class to set the DriverAge and Residency. They do error checking. They both have the same pattern. They will return false unless you set them correctly:
public bool SetResidency(ResidentState residency)
{
if (residency != ResidentState.OH && residency != ResidentState.MI)
{
Console.WriteLine("You can only enter states of OH or MI");
return false;
}
Residency = residency;
_residencyOk = true;
return true;
}
public bool SetDriverAge(int age)
{
if (age < 16 || age > 80)
{
Console.WriteLine("The driver's age must between 16 and 80.");
return false;
}
_ageOk = true;
DriverAge = age;
return true;
}
If the caller is using these correctly, then _ageOk and _residencyOk will be true. They may be false if not.
Then I create the CalculatePremium function to that class (and a few constants) (note, I renamed it to be verb-noun, I think it makes more sense):
private const int PremiumOhio = 100;
private const int PremiumMichigan = 250;
public int CalculatePremium()
{
if (!_residencyOk)
{
throw new ArgumentException("The driver's residency must first be entered, and only Ohio and Michigan are valid states");
}
if (!_ageOk)
{
throw new ArgumentException("The driver's age must first be entered, and it must be between 16 and 80");
}
int premium;
switch (Residency)
{
case ResidentState.MI:
premium = PremiumMichigan;
break;
case ResidentState.OH:
premium = PremiumOhio;
break;
default:
premium = 10_000;
break;
}
TotalPremium = (100 - DriverAge) * 3 + premium;
Console.WriteLine($"Your total premium is {TotalPremium:C}");
return TotalPremium;
}
Finally, I wrote the Program class that has the Main function:
class Program
{
static void Main(string[] args)
{
CarInsurance insurance = new CarInsurance();
bool ageOk = false;
do
{
Console.Write("Enter the age of the driver: ");
var response = Console.ReadLine();
ageOk = int.TryParse(response, out var age);
if (!ageOk)
{
Console.WriteLine("You must enter an integer for age");
}
else
{
ageOk = insurance.SetDriverAge(age);
}
} while (!ageOk);
bool stateOk = false;
do
{
Console.Write("Enter the state the driver lives in: ");
var stateStr = Console.ReadLine();
stateOk = Enum.TryParse<ResidentState>(stateStr, true, out var state);
if (!stateOk)
{
Console.WriteLine($"The state you entered ({stateStr}) is not a valid two-letter state abbreviation");
}
else
{
stateOk = insurance.SetResidency(state);
}
} while (!stateOk);
var premium = insurance.CalculatePremium();
}
}
Note that I check that the entered data is a valid integer (or a valid state abbreviation) in this code. Then I check (using the Insurance class) that it is valid for this calculation. As it's written, it will not cause either of the exceptions within CalculatePremium to be thrown. However, it might make sense to wrap the call to CalculatePremium in a try/catch. I'll leave that up to you - you really need to read up on exceptions.
I wrote this is a way (and explained it in a way), that will allow you to explore some of the features of the language. Step through it in the debugger. Enter invalid entries for both quantities (something that's not an integer, and then something that's out of range for age, and something that's not a state and then not either MI or OH) and see what happens. Try calling calculate premium with invalid values and see what happens (and try to catch the result).
I am trying to write a mini-quiz and I want the "try again" button to follow the same rules as the "if" statement as before the "else"
using System;
public class Program
{
public static void Main()
{
int x;
x = int.Parse(Console.ReadLine());
Console.WriteLine("Find a number that can be divided by both 7 and 12");
if ((x % 7 == 0) && (x % 12 == 0))
{
Console.WriteLine("well done, " +x+ " can be divided by 7 and 12");
}
else
{
Console.WriteLine("Wrong, try again.");
Console.ReadLine();
}
}
}
I want the ReadLine after the else statement to follow the same rules as the "if" statement before it but it needs a whole new statement to follow and copy-pasting the statement seems like an inefficient solution.
Normally this kind of processing is done in a while loop, which continues to loop until the user answers correctly. So the key is then to create a condition that will become false when there's a correct answer.
Notice that we also have the x variable re-assigned to the Console.ReadLine() method in the else block, otherwise we're always comparing the old value of x and the loop will never end.
For example:
bool answeredCorrectly = false;
while (!answeredCorrectly)
{
if ((x % 7 == 0) && (x % 12 == 0))
{
Console.WriteLine("well done, " + x + " can be divided by 7 and 12");
answeredCorrectly = true; // This will have us exit the while loop
}
else
{
Console.WriteLine("Wrong, try again.");
x = int.Parse(Console.ReadLine());
}
}
If you want to be really tricky about it, you could write a method that will get an integer from the user, and which takes function that can be used to validate that the input is correct (any method that takes in an int and returns a bool).
This way, you can create a validation method and pass that (along with the prompt for the user) to the method that gets an integer from the user.
Note that we're using the int.TryParse method to try to get an integer from the string input. This method is really handy because it does two things: First, it returns true if the parsing succeeds, and second, it returns the int value in an out parameter. This way we can use the return value to ensure they entered a number, and we can use the output parameter to see if the number meets our conditions:
private static int GetIntFromUser(string prompt, Func<int, bool> validator = null)
{
int result = 0;
bool answeredCorrectly = false;
while (!answeredCorrectly)
{
// Show message to user
Console.Write(prompt);
// Set to true only if int.TryParse succeeds and the validator returns true
answeredCorrectly = int.TryParse(Console.ReadLine(), out result) &&
(validator == null || validator.Invoke(result));
if (!answeredCorrectly) Console.WriteLine("Incorrect, please try again");
}
return result;
}
With this method in place, we can now call it from our main method as often as we like, with whatever validation we like, and we don't need to re-write all the looping code each time:
int x = GetIntFromUser("Enter a number that can be divided by both 7 and 12: ",
i => i % 7 == 0 && i % 12 == 0);
x = GetIntFromUser("Enter a negative number: ", i => i < 0);
x = GetIntFromUser("Enter a number between 10 and 20: ", i => i > 10 && i < 20);
You could even use it to create a number guessing game with just a few lines of code!
int randomNumber = new Random().Next(1, 101);
int x = GetIntFromUser("I'm thinking of a number from 1 to 100. Try to guess it: ", i =>
{
Console.WriteLine(i < randomNumber
? $"{i} is too low - guess a larger number."
: i > randomNumber ? $"{i} is too high - guess a smaller number." : "Correct!");
return i == randomNumber;
});
Have you considered using a while block and break; on the successful condition?
using System;
public class Program
{
public static void Main()
{
int x;
Console.WriteLine("Find a number that can be divided by both 7 and 12");
while (true)
{ //Loop the code until it is broken out of
x = int.Parse(Console.ReadLine());
if ((x % 7 == 0) && (x % 12 == 0))
{
Console.WriteLine("well done, " + x + " can be divided by 7 and 12");
Console.ReadKey(); //Pause the program so it doesnt break out immediately
break; //Break out of the while loop
}
else
{
Console.WriteLine("Wrong, try again.");
}
}
}
}
I am receiving an error "Operator '*' cannot be applied to operands of type 'int' and 'decimal[]'", as I am attempting to multiply two values with different data types (one being a value located in an array). My question is how am I able to multiple numberOfMinutes * perMinuteRate in my code below? My variable is called total, which I declared a double data type (although may be incorrect).
I tried changing data types and played with formatting (like ToString), but I am not sure what to do. I also tried to google the answer with no success.
I am by no means a professional programmer; I'm not in school. I'm a data analyst who is learning to program.
Here is my code:
static void Main(string[] args)
{
int[] areaCodes = { 262, 414, 608, 715, 815, 920 };
decimal[] perMinuteRate = { .07m, .1m, .05m, .16m, .24m, .14m };
int numberOfMinutes;
int userAreaCode;
string inputString = "1";
while (inputString != "0")
{
int x;
Console.WriteLine("Enter the area code for your call (or 1 to end):");
inputString = Console.ReadLine();
userAreaCode = Convert.ToInt32(inputString);
Console.WriteLine("How many minutes will your call last?");
inputString = Console.ReadLine();
numberOfMinutes = Convert.ToInt32(inputString);
for (x = 0; x < areaCodes.Length; x++)
{
if (userAreaCode == areaCodes[x])
{
***double total = numberOfMinutes * perMinuteRate;***
Console.WriteLine("You call to {0} will cost {1} per minute for a total of {2}.", areaCodes[x], perMinuteRate[x].ToString("C"), total.ToString("C"));
x = areaCodes.Length;
}
}
if (x != areaCodes.Length)
{
Console.WriteLine("I'm sorry; we don't cover that area.");
inputString = "1";
}
else
{
Console.WriteLine("Thanks for being our customer.");
inputString = "0";
}
Console.ReadLine();
}
}
Thank you in advance.
Change:
double total = numberOfMinutes * perMinuteRate;
to
double total = (double)(numberOfMinutes * perMinuteRate[x]);
The same way you index into perMinuteRate in the line directly below.
The expression [int] * [decimal] will result in a decimal, and the cast (double) will convert it to a double
To avoid loss of precision, change it to:
decimal total = numberOfMinutes * perMinuteRate[x];
I need to format a double value so that it fits within a field of 13 characters. Is there a way to do this with String.Format or am I stuck with character-by-character work?
Edits: (hopefully they will stay this time)
With cases greater than a trillion I am to report an error. It's basically a calculator interface.
My own answer:
private void DisplayValue(double a_value)
{
String displayText = String.Format("{0:0." + "".PadRight(_maxLength, '#') + "}", a_value);
if (displayText.Length > _maxLength)
{
var decimalIndex = displayText.IndexOf('.');
if (decimalIndex >= _maxLength || decimalIndex < 0)
{
Error();
return;
}
var match = Regex.Match(displayText, #"^-?(?<digits>\d*)\.\d*$");
if (!match.Success)
{
Error();
return;
}
var extra = 1;
if (a_value < 0)
extra = 2;
var digitsLength = match.Groups["digits"].Value.Length;
var places = (_maxLength - extra) - digitsLength;
a_value = Math.Round(a_value, places);
displayText = String.Format("{0:0." + "".PadRight(_maxLength, '#') + "}", a_value);
if (displayText.Length > _maxLength)
{
Error();
return;
}
}
DisplayText = displayText;
}
If this is calculator, then you can not use character-by-character method you mention in your question. You must round number to needed decimal places first and only then display it otherwise you could get wrong result. For example, number 1.99999 trimmed to length of 4 would be 1.99, but result 2 would be more correct.
Following code will do what you need:
int maxLength = 3;
double number = 1.96;
string output = null;
int decimalPlaces = maxLength - 2; //because every decimal contains at least "0."
bool isError = true;
while (isError && decimalPlaces >= 0)
{
output = Math.Round(number, decimalPlaces).ToString();
isError = output.Length > maxLength;
decimalPlaces--;
}
if (isError)
{
//handle error
}
else
{
//we got result
Debug.Write(output);
}
You have a lot formatting options using String.Format, just specify format after placeholder like this {0:format}.
Complete example looks like this:
Console.WriteLine("Your account balance is {0:N2}.", value);
Output would be:
Your account balance is 15.34.
All of the options for numeric types are listed here:
http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx
This seems to work for me (but is hand-rolled):
static string FormatDouble(double d)
{
int maxLen = 13;
double threshold = Math.Pow(10, maxLen);
if (d >= threshold || d <= 0 - (threshold/10))
return "OVERFLOW";
string strDisplay = "" + d;
if (strDisplay.Length > maxLen )
strDisplay = strDisplay.Substring(0, maxLen);
if (strDisplay.EndsWith("."))
strDisplay = strDisplay.Replace(".", "");
return strDisplay;
}
Let me know if it gives you trouble with scientific notation creeping in. I believe the format "{0:R}" should help you avoid that explicitly.
Also, I wasn't sure if you were including +/- sign in digit count or if that was in a separate UI slot.
The theory on rounding here is that, yes, "" + d might round some things, but in general it's going to be many more digits out than are ever displayed so it shouldn't matter. So this method should always truncate.
Here's a solution that does rounding. (I couldn't think of a non-mathematical way to do it):
static string FormatDouble(double d)
{
int maxLen = 13;
int places = (int)Math.Max(Math.Log10(Math.Abs(d)), 0);
places += (d == Math.Abs(d) ? 1 : 2);
if (places > maxLen || places < 1 - maxLen)
return "OVERFLOW";
if (Math.Floor(d) == d) ++places; // no decimal means one extra spot
d = Math.Round(d, Math.Max(maxLen - places - 1, 0));
return string.Format("{0:R}", d);
}
Note: I still think your users might appreciate seeing something closer to what is being stored in the underlying memory than what is often typical of calculators. (I especially hate the ones that can turn 0.99 into 1.01) Either way, you've got at least 3 solutions now so it's up to you.
Let's say I have this code:
int i = 31240;
string Number = ((double)i / 1000).ToString("0.#k");
I get this result as a string for Number: 31,2k
And now, I wanna do the exact opposite, that is to take this string "31,2k" and take it back to 31240 or even to 31200, but I don't know how to do...
Any idea?
Someone said that was impossible.
BUT Finally I found the perfect way to achieve my goal. I post the solution for those who could be willing to know.
The use is simple, and it allows to make 2 kind of conversions:
Thousands, Example: 45831 <=> 45,8k <=> 45831
Millions, Example: 123852376 <=> 123,5m <=> 123852376
int i = (int)(Double.Parse(Number.Substring(0, Number.Length - 1)) * 1000);
We remove the k with Number.Substring(0, Number.Length - 1), transform it to double with Double.Parse, multiply by 1000 and in the end convert to int. The order of the things is very important! The first time I was doing (int)Double.Parse(Number.Substring(0, Number.Length - 1)) * 1000 that was converting to int before multiplying (so I got 31000 instead of 31200)
I'll add that if I had to write that code, I would sleep VERY much better if I used the Decimal.Parse instead of the Double.Parse (so I would be sure against the vagaries of floating points)
I'll add a better method:
int i2 = int.Parse(Number.Substring(0, Number.Length - 1).Replace(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator, string.Empty)) * 100;
This is much more interesting. We remove the k as in the other method but this time we remove the , from the string too and we multiply by 100.
The interesting trick is that instead of simply ("bovinamente" in italian slang, as bovines would do) replace the , with an empty string, we get the current decimal separator (CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator) and we replace THAT with an empty string.
Clearly had we used another culture in composing the original string (for example the always good CultureInfo.InvariantCulture) we would use that to get the NumberDecimalSeparator
Here is the solution I went through:
public class StringFromInt
{
public enum FormatStyle
{
Kilo = 1000,
Mega = 1000000
}
public int Number;
public FormatStyle Format
{
get
{
switch (LimitValueBeforeConversion)
{
case 1000: return FormatStyle.Kilo;
case 1000000: return FormatStyle.Mega;
default:
throw new NotImplementedException("You must implement the code for this kind of value");
}
}
set
{
if (value == FormatStyle.Kilo)
{
LimitValueBeforeConversion = 1000;
}
else if (value == FormatStyle.Mega)
{
LimitValueBeforeConversion = 1000000;
}
}
}
public int LimitValueBeforeConversion
{ get; set; }
public static implicit operator int(StringFromInt s)
{
return s.Number;
}
public static implicit operator StringFromInt(int number)
{
StringFromInt s = new StringFromInt(number);
return s;
}
#region Constructors
public StringFromInt(int number, FormatStyle format)
{
this.Number = number;
Format = format;
}
public StringFromInt(int number)
: this(number, FormatStyle.Kilo)
{
if (number >= 1000000)
{
this.Format = FormatStyle.Mega;
}
}
#endregion
public override string ToString()
{
if (Number >= LimitValueBeforeConversion)
{
string formatString = "0.#k";
switch (Format)
{
case FormatStyle.Kilo:
formatString = "0.#k";
break;
case FormatStyle.Mega:
formatString = "0.#m";
break;
default:
throw new NotImplementedException("You must implement the code for this kind of value");
}
return ((double)Number / LimitValueBeforeConversion).ToString(formatString);
}
else
{
return Number.ToString();
}
}
}
And here is a test program:
class Program
{
static void Main(string[] args)
{
int i = 31240;
string StringRepresentation = ((double)i / 1000).ToString("0.#k");
int resultBackWithParse = int.Parse(StringRepresentation.Substring(0, StringRepresentation.Length - 1).Replace(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator, string.Empty)) * 100;
Console.WriteLine("Base number: " + i.ToString());
Console.WriteLine(new string('-', 35));
Console.WriteLine("String representation: " + StringRepresentation);
Console.WriteLine("Int representation With Int.Parse: " + resultBackWithParse.ToString());
Console.WriteLine();
StringFromInt MySolutionNumber = i;
int resultBackWithStringFromInt = MySolutionNumber;
Console.WriteLine("String representation With StringFromInt: " + MySolutionNumber.ToString());
Console.WriteLine("Int representation With StringFromInt: " + resultBackWithStringFromInt);
Console.WriteLine(new string('=', 35) + "\n");
i = 123456789;
StringFromInt MyNumber = 123456789;
int resultBack = MyNumber;
Console.WriteLine("Base number: " + i.ToString());
Console.WriteLine(new string('-', 35));
Console.WriteLine("String representation With StringFromInt: " + MyNumber);
Console.WriteLine("Int representation With StringFromInt: " + resultBack);
Console.ReadKey(true);
}
}
As you can notice, there is no need to use the "new" initializer, I mean no need to do:
StringFromInt Number = new StringFromInt(YourNumber)
Thanks to the implicit operator, you can do:
StringFromInt Number = YourNumber
I don't know, but I think it's a good beginning, what do you think?
Anyway, I managed to do what I wanted, so for people who thought it couldn't be done, you see, that's possible :-)
Obviously this can be improved: this version works for thousands and millions only.
Greetings