/// <summary> /// Converter a string em número decimal /// </summary> /// <param colName="o"></param> /// <returns></returns> public static Decimal ToDecimal(object stringValue, int scale = 4) { if (IsValidText(stringValue)) { try { //stringValue = stringValue.ToString().Replace(".", ","); stringValue = prepareStringToConvertDecimal(stringValue); var round = Math.Round(Convert.ToDecimal(stringValue), scale); return(round); } catch (OverflowException ex) { LoggerUtilIts.ShowExceptionLogs(ex); } catch (InvalidCastException ex) { Utils.ShowExceptionStack(ex); } catch (FormatException ex2) { Console.WriteLine(ex2.InnerException); } } return(0.000m); }/// <summary>
/// <summary> /// Converter a string em número inteiro /// </summary> /// <param colName="o"></param> /// <returns></returns> public static int ToInt(object stringValue) { if (IsValidText(stringValue)) { try { if (stringValue.ToString().Contains(",")) { stringValue = stringValue.ToString().Split(',')[0]; } return(Convert.ToInt32(stringValue)); } catch (OverflowException ex) { LoggerUtilIts.ShowExceptionLogs(ex); } catch (FormatException ex) { LoggerUtilIts.ShowExceptionMessage(ex); return(-1); } catch (Exception ex) { LoggerUtilIts.ShowExceptionLogs(ex); } } return(0); }
/// <summary> /// Converter a string em número decimal /// </summary> /// <param colName="o"></param> /// <returns></returns> public static Decimal ToDecimal(object stringValue) { if (IsValidText(stringValue)) { try { stringValue = prepareStringToConvertDecimal(stringValue); return(Convert.ToDecimal(stringValue)); } catch (OverflowException ex) { LoggerUtilIts.ShowExceptionLogs(ex); } catch (InvalidCastException ex) { Utils.ShowExceptionStack(ex); } catch (FormatException ex2) { Console.WriteLine(ex2.InnerException); } } return(0.000m); }
/// <summary> /// Retorna a mesma data com um mês a frente /// </summary> /// <param name="mes"></param> /// <returns></returns> public static DateTime GetNextDate(DateTime dtData) { int dia = dtData.Day; int mes = dtData.Month; int anos = dtData.Year; int diasDoMes = DateTime.DaysInMonth(anos, mes); //se o mes for o ultimo então vai pra o primeiro if (mes == 12) { mes = 1; //va para o primeiro anos++; //incremente o ano } else { mes++;//um mês a frente } //tratamento para ver se o dia data informada //se enquadra na proxima data try { var nextData = new DateTime(anos, mes, 1); int diaNextData = DateTime.DaysInMonth(anos, nextData.Month); if (dia > diaNextData) { Console.WriteLine("Dia setado possui range diferente a data sua frente"); dia = diaNextData; } var data = new DateTime(anos, mes, dia); return(data); } catch (ArgumentOutOfRangeException ex) { if (dia == 31) { dia = 1; } else { dia++; } Console.WriteLine("A data corrente nao pode ter estar no dia " + dia); LoggerUtilIts.ShowExceptionLogs(ex); } return(new DateTime(1900, 1, 1)); }
/// <summary> /// Retorna os bytes do arquivo /// </summary> /// <param name="file"></param>Arquivo /// <returns></returns>bytes do arquivo public static byte[] GetBytesFromFile(string file) { try { //Create a file stream from an existing file. FileInfo fi = new FileInfo(file); FileStream fs = fi.OpenRead(); return(GetBytesFromStream(fs)); } catch (Exception ex) { LoggerUtilIts.ShowExceptionLogs(ex); return(null); } }
/// <summary> /// Obtem a imagem do arquivo /// Metodo nativo /// </summary> /// <param name="file"></param> /// <returns></returns> public static Image GetImageFromFile(string file) { try { return(Image.FromFile(file)); } // The System.Drawing.Image this method creates. //T: System.OutOfMemoryException: //T: System.IO.FileNotFoundException: //T: System.ArgumentException: catch (Exception ex) { LoggerUtilIts.ShowExceptionLogs(ex); return(null); } }
/// <summary> /// Converter a string em data /// </summary> /// <param name="value"></param> /// <returns></returns> public static DateTime?ToDateOrNull(object value) { if (IsValidText(value) && InstanceOfString(value)) { if (value.GetType() == typeof(DateTime)) { try { return(Convert.ToDateTime(value)); } catch (FormatException ex) { LoggerUtilIts.ShowExceptionLogs(ex); } } } return(null); }
/// <summary> /// Converter uma string em Double /// </summary> /// <param colName="o"></param> /// <returns></returns> public static Double ToDouble(object stringValue) { if (IsValidText(stringValue)) { try { stringValue = stringValue.ToString().Replace(".", ","); return(Convert.ToDouble(stringValue)); } catch (OverflowException ex) { LoggerUtilIts.ShowExceptionLogs(ex); } catch (FormatException ex) { LoggerUtilIts.ShowExceptionLogs(ex); } } return(0.0d); }
/// <summary> /// Converter uma string em Float /// </summary> /// <param colName="o"></param> /// <returns></returns> public static Single ToFloat(object stringValue) { if (IsValidText(stringValue)) { try { //stringValue = stringValue.ToString().Replace(".", ","); stringValue = prepareStringToConvertDecimal(stringValue); return(Convert.ToSingle(stringValue)); } catch (OverflowException ex) { LoggerUtilIts.ShowExceptionLogs(ex); } catch (FormatException ex) { LoggerUtilIts.ShowExceptionLogs(ex); } } return(0.0f); }
/// <summary> /// Cria uma imagem a partir do arquivo informado. /// </summary> /// <param name="file"></param> /// <returns></returns> public static Image CreateImage(string file) { try { using (var ms = new MemoryStream()) { var imfFormart = System.Drawing.Imaging.ImageFormat.Jpeg; var bmp = new Bitmap(file); return(bmp); } } // The System.Drawing.Image this method creates. //T: System.OutOfMemoryException: //T: System.IO.FileNotFoundException: //T: System.ArgumentException: catch (Exception ex) { LoggerUtilIts.ShowExceptionLogs(ex); return(null); } }
/// <summary> /// Converter a string em número inteiro (long) /// </summary> /// <param colName="o"></param> /// <returns></returns> public static long ToLong(object stringValue) { if (IsValidText(stringValue)) { try { return(Convert.ToInt64(stringValue)); } catch (OverflowException ex) { LoggerUtilIts.ShowExceptionLogs(ex); } catch (FormatException ex) { LoggerUtilIts.ShowExceptionLogs(ex); return(-1); } catch (Exception ex) { LoggerUtilIts.ShowExceptionLogs(ex); } } return(0); }