PGTO formula in c# - c#

i need to use this formula in c#, and i dont know how is the original formula.
can anyone help me?

PGTO is actually the Portuguese name for the PMT Excel function (found it out with this handy Excel function names translation!
The Microsoft.VisualBasic namespace has the Financial.PMT method, but of course you can use it in C#:
Returns a Double specifying the
payment for an annuity based on
periodic, fixed payments and a fixed
interest rate

Related

Extracting Slope & Intercept from a Scatterplot Trendline Programmatically

I cannot seem to find a built in property for Excel Chart Trendlines where I can grab the slope or the intercept. It allows me to display the trendline equation on the chart itself in excel, but I can't access it within the code. Anyone know any property or method that I may have been overlooking? I can always calculate the equation separately, it just seems like a waste if Excel already has it, but I can't access it.
I kindof found what I was looking for. I would ideally like to extract the trendline directly from the Excel chart, but I found a way to access excel functions within the code. Forward Ed put me on the right track.
using IExcel = Microsoft.Office.Interop.Excel;
Then declare and initialise a IWorkSheetFunction object
IExcel.IWorksheetFunction iwf;
Now you can use the functions as in
double result = iwf.NormDist(1,2,3,4);

Find Critical Chi Square Value using MathNet.Numerics

So I want to get Critical Chi-Square Value using Significance level and Degrees of Freedom. I tried using MathNet.Numerics but couldn't find which method to use to get the Critical Chi-Square Value
This was the documentation I'm referring, any help on redirecting me to correct documentation would help.
How I calculate the value in Excel is by using the formula =CHISQ.INV.RT(A2,B2)
The function you require is InvCDF(), it is used as follows:
MathNet.Numerics.Distributions.ChiSquared.InvCDF(degreesOfFreedom, probability);
I could finally solve this problem, so I want to share how I solved it.
I used the MathNet library, and to use the same function of Excel you are providing you have to keep in mind a few things: in this library it does not exist =CHISQ.INV.RT itself, instead, in C#, you need to use InvCDF (the equivalent of =CHISQ.INV in Excel) but instead of using a probability parameter like 0.05, you have to use the opposite part of the interval (0, 1), so that parameter should be 0.95.
The logic of this is in the description of the functions in Excel.
"CHISQ.INV" description says "Returns the inverse of the left-tailed probability of the chi-squared distribution", this one is the equivalent of ChiSquared.InvCDF (C#).
"CHISQ.INV.RT" description says "Returns the inverse of the right-tailed probability of the chi-squared distribution", this one DOES NOT exist in the MathNet library.
Example:
In Excel you write
=CHISQ.INV.RT(0.05, 9)
In C# you write
ChiSquared.InvCDF(9, 0.95);
In both cases the answer will be 16.9189776
Note that the order of the parameters are switched.
I hope I could help with this.

Mathematical Expression using NCalc

I am trying understand the expression library NCalc.
http://ncalc.codeplex.com
If I have a script as below,
SET A = CLOSE - OPEN;
SET B = A>0.5 AND CLOSE > HIGH
If(HIGH > 5, ROC(CLOSE), B)
CLOSE is a List of double,
OPEN is a List of double,
HIGH is a List of double
AND ROC is a custom function which takes any List of double as input parameter and returns a list as output.
Can someone advice me if I can use NCalc library to parse this script? I couldn't find any documentation at Codeplex website.
Is there any where I could obtain the help file or documentation for NCalc?
Thanks a million.
yes you can. first two will be very straightforward for third you will just have to create your custom IF and ROC function

sql type float, real, decimal?

well in my database i had a colum for price of one product
i had it as float, my problem is if i saved it since my c# application
as 10.50 .. in a query it returns 10,50 and if i update i get a error
10,50 cant convert to float ... or something so..
and if i saved it as decimal, in queries inside sql management .. are ok..
but in my c# application... i get the same error..
10.50 retuns as 10,50 i dont know why, and how to solved it.. my unique solution is saved it
as varchar...
That's a localisation problem of some sort. 10,50 is the "European" way of writing ten and a half. If you're getting that from your select statements then your database is probably configured incorrectly.
Generally speaking you should use the same type throughout your layers. So if the underlying types in the database are x, you should pass around those data with identical types in c#, too.
What type you choose depends on what you are storing--you shouldn't be switching around types just to get something to "work". To that end, storing numeric data in a non-numeric type (e.g. varchar) will come back to bite you very soon. It's good you've opened this question to fix that!
As others have miraculously inferred, you are likely running into a localization issue. This is a great example of why storing numbers as strings is a problem. If you properly accept user input in whatever culture/localization they want (or you want), and get it into a numeric-type variable, then the rest (talking to the DB) should be easy. More so, you should not do number formatting in the database if you can help it--that stuff is much better placed at the front end, closer to the users.
I think your setting in windows regional and language for decimal symbol is wrong.please set it to dot and again test it.
This may help out for temporary use but I wouldn't recommend it for permanent use:
Try making it so that just before you save the file, convert the number to a string, replace the commas with periods (From , to .) and then save it into the database as the string, hopefully it should see that it is in the correct format and turn it into what the database sees as "Decimal" or "Floating".
Hope this helps.
Yep, localization.
That said, I think your pice is being stored on a "money" field in SQLServer (I'm assuming it's SQLServer you're using). If that was a float in the DB, it would return it with a normal decimal point, and not the European money separator ",".
To fix:
Fist DO NO USE FLOAT in your c# code, unless you absolutely require a floating point number. Use the decimal type instead. That's not just in this case, but in all cases. Floating point numbers are binary (base-2), not decimal (base-10), so what you see in the interface is only a decimal approximation of the actual number. The result is that frequently (1 == 1) evaluates as false!
I've run into that problem myself, and it's maddening if you don't know that can happen. Always use decimal instead of float in c#.
Ok, after you've fixed that, then do this to get the right localization:
using System.Globalization;
...
NumberFormatInfo ni = new NumberFormatInfo();
ni.CurrencyDecimalSeparator = ",";
decimal price = decimal.Parse(dbPriceDataField, ni);
Note that "dbPriceDataField" must be a string, so you may have to do a ".ToString()" on that db resultset's field.
If you end up having to handle other "money" aspects of that money field, like currency symbols, check out: http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.aspx
If you need more robust error handling, either put that decimal.Parse in a try/catch, or use decimal.TryParse.
EDIT --
If you know what culture (really, country), the db is set to, you can do this instead:
using System.Globalization;
...
CultureInfo ci = new CultureInfo("fr-FR"); // fr-FR being "french France"
decimal price = decimal.Parse(dbprice, ci.NumberFormat);
Such problems were faced by me in my Web Apps... but i found the solution like I was fetching my price value in textbox. So I was have database attached with that. So when you attached your database with textbox... When you right click textbox and click Edit DataBinding.... in that you have to provide.... type like in Bind Property..... {0:N2}
This will work only for web apps or websites... not for desktop applications...

Regex and Excel Cells

There is a .NET opensource library called NPOI that allows you to manipulate Excel files. Unfortunately, their ShiftRows function does not adjust any cell references in formulas.
Therefore, I need to create a regex pattern to update them. Take for example a cell containing the following formula:
=(B7/C9) * (A10-B4)
I would like to bump any row references by 1 thus becoming
=(B8/C10) * (A11-B5)
Basically, I just need a pattern that will extract the numbers out into a "MatchCollection". I can do the rest.
Can anyone help?
Thanks.
Take a look at my answer to another question: Which regular expression is able to select excel column names in a formula in C#?
In that answer I match formulas and include code to increment the cell number (and column naming). Also, do a search on Excel column naming here and you'll find other ways to get the names generated or incremented. I probably could shorten the IncrementColumn MatchEvaluator's code using one of those methods but that's what I came up with at the time.
You should be very careful using regular expressions for this purpose.
Excel formulas can become very complex, especially with user-defined functions or when functions apply to ranges.
NPOI contains a Formula evaluation library and I think you should have a look at this as well - especially if the spreadsheets are out of your control.

Categories