C
C#6mo ago
noobmaster69

✅ Exception thrown: 'System.FormatException' in mscorlib.dll

im just trying to output the data out
No description
67 Replies
Buddy
Buddy6mo ago
What datatype is monthr? Also I recommend using string interpolation fully, that is insanely hard to read
noobmaster69
noobmaster69OP6mo ago
monthr is a string into int
leowest
leowest6mo ago
$interpolation
MODiX
MODiX6mo ago
String interpolation is the preferred way of building strings in C#. It is easier to read than concatenation. For example:
var foo = 1;
var bar = 2;
Console.WriteLine("foo is equal to: " + foo + " and bar is equal to: " + bar);
var foo = 1;
var bar = 2;
Console.WriteLine("foo is equal to: " + foo + " and bar is equal to: " + bar);
can be written as:
var foo = 1;
var bar = 2;
Console.WriteLine($"foo is equal to: {foo} and bar is equal to: {bar}");
var foo = 1;
var bar = 2;
Console.WriteLine($"foo is equal to: {foo} and bar is equal to: {bar}");
noobmaster69
noobmaster69OP6mo ago
No description
noobmaster69
noobmaster69OP6mo ago
im just doing whatever get me homr I’ll tr this
cap5lut
cap5lut6mo ago
double isnt an integral type
noobmaster69
noobmaster69OP6mo ago
Fork Is there one for double? Caz I need to output as 01 02 etc and I also need to use that to cal something too
cap5lut
cap5lut6mo ago
why even use the double type when u r only using it as int?
noobmaster69
noobmaster69OP6mo ago
Can I use Int to cal in double? like double = Int + 0.5
cap5lut
cap5lut6mo ago
tho to answer ur question, u could simply use an explicit format {monthr:00} but i can explain that better on some other explicit format #00.00# # means its an optional digit, 0 is an explicit digit so for 1.5 as value u would get 01.50
noobmaster69
noobmaster69OP6mo ago
{monthr:00} work for both Int and double?
cap5lut
cap5lut6mo ago
yeah
noobmaster69
noobmaster69OP6mo ago
noice lemme try it Can you answer this tho?
noobmaster69
noobmaster69OP6mo ago
No description
noobmaster69
noobmaster69OP6mo ago
getiing somewhere
cap5lut
cap5lut6mo ago
in arithmetic u can mix the number types, as long as u wont lose precision, so double d = someInt + 0.5; the someInt would be first be "upcasted" to a double, and then u simply have only double values there
noobmaster69
noobmaster69OP6mo ago
ic ic thanks
cap5lut
cap5lut6mo ago
also, u shouldnt use double but decimal for monetary calculations
noobmaster69
noobmaster69OP6mo ago
how to make 10.208328308 abit shorter to 10.50? no tis okay i actaualy want to round those number later
cap5lut
cap5lut6mo ago
check out F
noobmaster69
noobmaster69OP6mo ago
i see it now
cap5lut
cap5lut6mo ago
thats independent of each other. the "normal" floating point types (float and double) use an someValue * 2^someOtherValueinternally to represent a number, decimal uses 10 internally, which is more accurate for monetary calculations
noobmaster69
noobmaster69OP6mo ago
oh alright
cap5lut
cap5lut6mo ago
u can observe this for example here:
MODiX
MODiX6mo ago
cap5lut
REPL Result: Success
Console.WriteLine(0.1 + 0.2);
Console.WriteLine(0.1m + 0.2m);
Console.WriteLine(0.1 + 0.2);
Console.WriteLine(0.1m + 0.2m);
Console Output
0.30000000000000004
0.3
0.30000000000000004
0.3
Compile: 402.499ms | Execution: 23.600ms | React with ❌ to remove this embed.
cap5lut
cap5lut6mo ago
m is the suffix to use the number as decimal type, just like f is used for float
noobmaster69
noobmaster69OP6mo ago
I just need to put :m like this right? I just try to use declare all double into decimal but that would cause some error if it’s too much trouble then I’m okay with this caz It actually doing what I want them to
cap5lut
cap5lut6mo ago
yeah, because the numbers u have in code like for example 1.5 are still doubles, u have to use the m suffix there eg float someFloat = 1.5; would not compile either, because 1.5 is a double value, float someFloat = 1.5f; would compile tho and ofc decimal someDecimal = 1.5m;
noobmaster69
noobmaster69OP6mo ago
hm
cap5lut
cap5lut6mo ago
for some homework or private training project it will be fine to use double i assume
noobmaster69
noobmaster69OP6mo ago
Yea it’s an assignment will have to hand it over tmr
noobmaster69
noobmaster69OP6mo ago
hey im sorry to bother you but can you explain why is the final installment is not 10.56 instead it goes down by 0.01 to 10.55
No description
noobmaster69
noobmaster69OP6mo ago
double installment = (loan * interestRate) / (1 - Math.Pow((1 + interestRate), -month)); to be clear this is not my work its what im aiming toward
cap5lut
cap5lut6mo ago
can u print the value without any formating, as in just Console.WriteLine(theValue);? and the respective $code
MODiX
MODiX6mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
noobmaster69
noobmaster69OP6mo ago
if (int.TryParse(inputs[0], out int number1) && int.TryParse(inputs[1], out int number2) && int.TryParse(inputs[2], out int number3))
{
if (number1 < 31 || number2 < 12)
{
if (number1 > 0 || number2 > 0 || number3 > 0)
{
double m = monthe;
int monthr = number2;
int yearr = number3;
double debtbal = loan;
double interest = loan * interestrate;
Console.WriteLine("\nMonth\t\tInstallment\tPrincipal\tInterest\tDebt Balance");
for (double i = 0; i < m; i++)
{
monthr = monthr + 1;
double installment = (loan * interestrate) / (1 - Math.Pow((1 + interestrate), -monthe));

interest = debtbal / 100;
double principle = installment - interest;
debtbal = debtbal - principle;

double interesrate = debtbal / 100;
if (monthr > 12)
{
monthr = 1;
yearr = yearr + 1;
}
Console.WriteLine($"{number1}-{monthr:00}-{yearr}\t${installment:f}\t\t${principle:f}\t\t${interest:f}\t\t${debtbal:f}");
}
}else
Console.WriteLine("Invalid input. Please enter three integers separated by a -.");
}else
{
Console.WriteLine("Invalid input. Please enter three integers separated by a -.");
}

}
else
{
Console.WriteLine("Invalid input. Please enter three integers separated by a -.");
}
if (int.TryParse(inputs[0], out int number1) && int.TryParse(inputs[1], out int number2) && int.TryParse(inputs[2], out int number3))
{
if (number1 < 31 || number2 < 12)
{
if (number1 > 0 || number2 > 0 || number3 > 0)
{
double m = monthe;
int monthr = number2;
int yearr = number3;
double debtbal = loan;
double interest = loan * interestrate;
Console.WriteLine("\nMonth\t\tInstallment\tPrincipal\tInterest\tDebt Balance");
for (double i = 0; i < m; i++)
{
monthr = monthr + 1;
double installment = (loan * interestrate) / (1 - Math.Pow((1 + interestrate), -monthe));

interest = debtbal / 100;
double principle = installment - interest;
debtbal = debtbal - principle;

double interesrate = debtbal / 100;
if (monthr > 12)
{
monthr = 1;
yearr = yearr + 1;
}
Console.WriteLine($"{number1}-{monthr:00}-{yearr}\t${installment:f}\t\t${principle:f}\t\t${interest:f}\t\t${debtbal:f}");
}
}else
Console.WriteLine("Invalid input. Please enter three integers separated by a -.");
}else
{
Console.WriteLine("Invalid input. Please enter three integers separated by a -.");
}

}
else
{
Console.WriteLine("Invalid input. Please enter three integers separated by a -.");
}
$codegif
noobmaster69
noobmaster69OP6mo ago
you know what its already pretty close to what i set out to do
No description
noobmaster69
noobmaster69OP6mo ago
just 0.01 dollar so i dont think its matter but how do i make it so that whenever i use \t it instead replace the space between the tab with -? like this
cap5lut
cap5lut6mo ago
well, that doesnt show anything about the total interest (nor ur screenshot)
noobmaster69
noobmaster69OP6mo ago
ah i can easily do the total
cap5lut
cap5lut6mo ago
i never really dabbled that much into formating, but most likely u would have to do that manually
noobmaster69
noobmaster69OP6mo ago
but the finall installment why is it 10.55
No description
cap5lut
cap5lut6mo ago
oh there, didnt notice that one
noobmaster69
noobmaster69OP6mo ago
ic ic i'll do it manaully i just hope that its symmetrical yea that one i dunno how he do it using the formula he provided this one but for me its a one and done nvr bring the formula to cal again
cap5lut
cap5lut6mo ago
well there are methods on string for padding, basically u would do something like string paddedString = yourDoubleValue.ToString("F2").PadRight(17, '-') to get ur value formatted and then padded on the right side with -s until it has a length of 17 or greater https://learn.microsoft.com/en-us/dotnet/api/system.string.padleft?view=net-8.0 https://learn.microsoft.com/en-us/dotnet/api/system.string.padright?view=net-8.0
noobmaster69
noobmaster69OP6mo ago
i dont get it but i'll mess around with it
cap5lut
cap5lut6mo ago
that the last one is different seems more like an imprecision that comes when doing calculations with floating point types
noobmaster69
noobmaster69OP6mo ago
that would be he calculate it multiple time
cap5lut
cap5lut6mo ago
well, not much i can say besides that when just seeing a screenshot *shrugs*
noobmaster69
noobmaster69OP6mo ago
yea 😂 nope i couldnt do it if i were to add padding using string then i would have to convert the double into string then i wouldnt beable to use :f to show .00 manual is not an option as well caz its not symmetical
cap5lut
cap5lut6mo ago
$"{someDouble:blahblah}" is the same as someDouble.ToString("blahblah")
MODiX
MODiX6mo ago
cap5lut
REPL Result: Success
double someDouble = 1.234567;
someDouble.ToString("F2").PadRight(17, '-')
double someDouble = 1.234567;
someDouble.ToString("F2").PadRight(17, '-')
Result: string
1.23-------------
1.23-------------
Compile: 315.168ms | Execution: 26.450ms | React with ❌ to remove this embed.
noobmaster69
noobmaster69OP6mo ago
omg letmme try it
noobmaster69
noobmaster69OP6mo ago
just look at the stuff im trying
No description
noobmaster69
noobmaster69OP6mo ago
doesnt seem to work
No description
noobmaster69
noobmaster69OP6mo ago
No description
noobmaster69
noobmaster69OP6mo ago
i mustve cout wrong
cap5lut
cap5lut6mo ago
someDouble.ToString("F2").PadRight(17, '-'); returns a string if u just use it like that u just discard the string. u would do something like,
string installmentColumn = installment.ToString("F2").PadRight(17, '-');
Console.WrilteLine($"date-stuff {installmentColumn} same for the rest");
string installmentColumn = installment.ToString("F2").PadRight(17, '-');
Console.WrilteLine($"date-stuff {installmentColumn} same for the rest");
im sorry, i should have explained that better
noobmaster69
noobmaster69OP6mo ago
alright ty no its okay yep its work ty so much thats all for now tysm for helping me i'll be bek in a while to send the final result
noobmaster69
noobmaster69OP6mo ago
No description
cap5lut
cap5lut6mo ago
oh one more thing if (number1 > 0 || number2 > 0 || number3 > 0), im pretty sure that all of these numbers have to be greater than zero, right? || is "or" and same thing for if (number1 < 31 || number2 < 12)
noobmaster69
noobmaster69OP6mo ago
Yes || is or Same thing with if Sorry. I didn’t reply sooner I went to bed asa I posted it
Want results from more Discord servers?
Add your server