How to calculate T.INV() in c# without excel workbook object - excel

How to calculate T.INV() in c# without excel workbook object. Is there any direct formula or class that can help me?.I have already Used "System.Web.UI.DataVisualization " namespace but it is not giving me expected result.
Thanks
Shakeb Khan
public static double InverseTDistributionfun(int DF, double probability)
{
double Tinv;
try
{
chart.Charting.Chart objChart = new chart.Charting.Chart();
Tinv = objChart.DataManipulator.Statistics.TDistribution(probability,DF, true);
// value = c16 + dup * y30;
}
catch (Exception ex)
{
throw ex;
}
return Tinv;
}
[Microsoft.SqlServer.Server.SqlProcedure]
public static double SQLCLRCsharpSP(int DF, double probability)
{
double _sum = 0.00;
try
{
//if (type == "C")
_sum = InverseTDistributionfun(DF, probability);
//if (type == "A")
//_sum = AlgTInvFun(probability, DF);
}
catch (Exception ex)
{
throw ex;
}
return _sum;
}

You can look the exact way up in Math.NET (MIT/X11).
double result = MathNet.Numerics.ExcelFunctions.TInv (probability, degFreedom);
which is the same as:
double result = -StudentT.InvCDF(0d, 1d, degFreedom, probability/2);
You find the complete solution here.
If that doesn't return the same values, then you're not using the tinv function.

Related

Apply decimal format to a CellType.Formula

I've recently update my POI lib to 3.17 and since then when I have a CellType.FORMULA I can't do the following:
public static void writeDecimal(Cell cell) {
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator('.');
String pattern = "#0.00";
DecimalFormat decimalFormat = new DecimalFormat(pattern, symbols);
decimalFormat.setParseBigDecimal(true);
BigDecimal bigDecimal;
try {
bigDecimal = (BigDecimal) decimalFormat.parse(cell.getStringCellValue());
Double value = bigDecimal.doubleValue();
cell.setCellType(CellType.NUMERIC);
cell.setCellValue(value);
} catch (ParseException e) {
Logger.getGlobal().log(Level.FINE, e.getMessage(), e);
}
}
I get this error Cannot get a STRING value from a NUMERIC formula cell, because of this bigDecimal = (BigDecimal) decimalFormat.parse(cell.getStringCellValue());
With 3.7 library I was able to execute this.
Thanks in advance
The issue is that the cell in the file itself is formatted to be a numeric cell. Assuming this cell will either always be a String or a Number, you should be able to modify your code just a little bit to get it to work.
You'll just need to check if it is a String cell first, and if it is, do things the way you're currently doing them. If it is a Numeric cell, you'll need to make your new BigDecimal from that number. I haven't worked much with BigDecimal, but I think the following code should work:
try {
if(cell.getCellTypeEnum() == CellType.STRING) {
bigDecimal = (BigDecimal) decimalFormat.parse(cell.getStringCellValue());
} else {
//Assuming here that cell.getCellTypeEnum() == CellType.NUMERIC
bigDecimal = new BigDecimal(cell.getNumericCellValue());
}
Double value = bigDecimal.doubleValue();
cell.setCellType(CellType.NUMERIC);
cell.setCellValue(value);
} catch (ParseException e) {
Logger.getGlobal().log(Level.FINE, e.getMessage(), e);
}
As an aside, is the BigDecimal necessary here? You seem to create it and then just get the Double value from it. You might be able to optimize this code by removing the BigDecimal stuff.

How do I import an Excel data table using FileHelpers and ExcelNPOIStorage?

I'm attempting to import a simple Excel data table into a strongly typed IEnumerable in C#. I'm using FileHelpers ExcelNPOIStorage engine to do this, assuming this won't require Excel to be installed on the user's machine (as the deprecated ExcelStorage option requires).
private T[] UseExcelNPOIStorage(string filename, bool skipFirstRow = true, int startColumn = 1)
{
ExcelNPOIStorage provider = new ExcelNPOIStorage(typeof(T), filename, skipFirstRow ? 1 : 0, 1);
provider.Progress += Provider_Progress;
provider.ExcelReadStopBehavior = ExcelReadStopBehavior.StopOnEmptyRow;
provider.ExcelReadStopAfterEmptyRows = 2;
provider.StartRow = skipFirstRow ? 1 : 0;
try
{
return (T[])provider.ExtractRecords();
}
catch (Exception ex)
{ }
return null;
}
When using the method listed above, I receive an "Index was outside the bounds of the array" exception. Am I missing something?

How to get the integer value in TextBox?

I have one Text Box in windows application. This Text Box only allowed the integer value not string. Can anybody have solution ?
Convert it.
public int GetIntValue(TextBox tb)
{
try
{
return Convert.toInt32(tb.Text);
}
catch (Exception ex)
{
//This is called if the converting failed for some reason
}
return 0; //This should only return 0 if the textbox does not contain a valid integer value
}
Use it like this:
int number = GetIntValue(textBox1);
Hope this helps!
Use this.
int value = Convert.ToInt32(textBox1.Text);
You use this code and get your integer value.Thanks
I found a solution from C# How do I make a textbox that only accepts numbers
Hope it will help you.
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar)
&& !char.IsDigit(e.KeyChar)
&& e.KeyChar != '.')
{
e.Handled = true;
}
// only allow one decimal point
if (e.KeyChar == '.'
&& (sender as TextBox).Text.IndexOf('.') > -1)
{
e.Handled = true;
}
}

searching in List<>

i want to find value in List<> but i am not getting the integer value. Here is my code from that i want to find the value in the List
private void txtnapsaserach_TextChanged(object sender, EventArgs e)
{
try
{
//decimal find = decimal.Parse(txtnapsaserach.Text);
if (decimal.Parse(txtnapsaserach.Text) > 0)
{
List<NapsaTable> _napsatabs = this.napsaTableBindingSource.List as List<NapsaTable>;
this.napsaTableBindingSource.DataSource =
_napsatabs.Where(p =>p.NapsaRate.Equals(txtnapsaserach.Text)).ToList();
}
}
catch (Exception Ex)
{
}
}
any solution for me . Because this works for me when i try to find string value.
private void txtnapsaserach_TextChanged(object sender, EventArgs e)
{
float value;
if (!float.TryParse(txtnapsaserach.Text, out value))
return; // return if text cannot be parsed as float number
if (value > 0)
{
var napsatabs = napsaTableBindingSource.List as List<NapsaTable>;
napsaTableBindingSource.DataSource =
napsatabs.Where(p =>p.NapsaRate == value).ToList();
}
}
try this
i want to find value in List<> but i am not getting the integer value.
Your p.NapsaRate is either integer type or floating point number, (probably decimal) Convert your txtnapsaserach.Text to decimal value and then compare it in where clause.
decimal rate = 0;
if(!decimal.TryParse(txtnapsaserach.Text), out rate)
{
//Invalid number in textbox
}
this.napsaTableBindingSource.DataSource =
_napsatabs.Where(p =>p.NapsaRate == rate)).ToList();
if p.NapsaRate is of type double or float you can parse them accordingly using Double.TryParse or Double.Parse etc
The reason you are not getting any error is that you are using object.Equals method for comparing decimal value with string. You should always use == for equality comparison of value types.

Cannot convert lambda expression to type 'int' because it is not a delegate type

In this c# code I need to convert the userName value from string to int type.
Is anyone know please help me. I have got error as a compilation error "Cannot convert lambda expression to type 'int' because it is not a delegate type" like this.
ShoppingCartPartRecord cartRecord = null;
try {
cartRecord = _shoppingCartRepository.Get(r => r.Username == userName);
}
catch (InvalidOperationException ex) {
if (ex.Message == "Sequence contains more than one element") {
var badCarts = _shoppingCartRepository.Table.Where(x => x.Username == userName);
foreach (var shoppingCartPartRecord in badCarts) {
_shoppingCartRepository.Delete(shoppingCartPartRecord);
}
}
}
Thank you in advance.
Without the source to your repository we can only guess at what the methods do.
From the errors you are describing the get function expects either an index into an array or an integer primary key and so is the wrong function
You should be able to change the code as follows to achieve the desired effect
ShoppingCartPartRecord cartRecord = null;
try {
cartRecord = _shoppingCartRepository.Table.Single(r => r.Username == userName);
}
catch (InvalidOperationException ex) {
if (ex.Message == "Sequence contains more than one element") {
var badCarts = _shoppingCartRepository.Table.Where(x => x.Username == userName);
foreach (var shoppingCartPartRecord in badCarts) {
_shoppingCartRepository.Delete(shoppingCartPartRecord);
}
}
}

Resources