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.
Related
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.
I am trying to understand how this convertingStringToInt method works. I am reading a file, storing the values in an array and am to pass those values to the method to be converted. In the parameters of convertingStringToInt, I have (String number) I don't get where the String "number" is getting its values. So I am passing in a string called numbers, but how is that newly created String associated with any of the values in my file...?!?
I am trying to understand the cause all the return numbers are the error code -460 except the last digit in the file. So the String numbers is associated with the file somehow I just don't get how...
public static void read_file()
{
try {
File file = new File("randomNumbers.txt");
Scanner scan = new Scanner(file);
int amountOfNumbersInFile = convertingStringToInt(scan.nextLine()); // read the first line which is 100 to set array size
global_numbers = new int[amountOfNumbersInFile]; // set the array size equal to the first line read which is 100
for (int index = 0; index < amountOfNumbersInFile; index++)
{
String line = scan.nextLine();
global_numbers [index] = convertingStringToInt(line);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
public static int convertingStringToInt(String numbers) //what does string "number" equal? why/where is it declared?
{
String numbers = scan.nextInt();
try {
return Integer.parseInt(numbers);
} catch (NumberFormatException n) {
return -460;
}
}
I have global_numbers declared as a global variable.
so the first thing u need understand is what u have in your txt file
if in this file you have only number is ok use stringToInt
but if you have words this never work properly
I am trying to get a valid integer from the user input, the function I wrote must display an error message for these inputs..
1) 0 or a negative number
2) a string
It works for strings but not for 0 or negative numbers
I am unable to find my mistakes.. :-(
public int getValidNumber()
{
int temp_int;
while ((!int.TryParse(Console.ReadLine(), out temp_int)) && (temp_int > 0))
{
Console.WriteLine("Not a valid number....try again");
}
return temp_int;
}
Is it the correct way to use temp_int variable immediately....?
The most obvious mistake is that your while runs if the int is greater than 0, which you don't want:
//Assign to temp_int first:
int.TryParse(Console.ReadLine(), out temp_int);
while (temp_int < 1)
{
Console.WriteLine("Not a valid number....try again");
int.TryParse(Console.ReadLine(), out temp_int);
}
getValidNumber(Console.ReadLine());
...
public int getValidNumber(string input)
{
int temp_int = int.TryParse(input, out temp_int);
if (temp_int =< 0)
{
Console.WriteLine("Not a valid number....try again");
}
return temp_int;
}
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.
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;
}
}