PDA

View Full Version : سه رقم سه رقم جدا کردن در.....



mohammaduir
چهارشنبه 09 مهر 1399, 18:43 عصر
سلام من برای گزارش گیریم از این قطعه کد استفاده کرده ام


e.Graphics.DrawString((GridRow.Cells(int).Value.To String), GridRow.Cells(int).InheritedStyle.Font, _
New SolidBrush(GridRow.Cells(int).InheritedStyle.ForeC olor), _
New RectangleF(ColumnLefts(i), tmpTop, ColumnWidths(i), _
CellHeight), StrFormat)

حالا اگه بخوابم فرمت عددم رو به سه رقم جدا تبدیل کنم چیکار باید کنمم؟ (GridRow.Cells(int).Value.ToString

the king
چهارشنبه 09 مهر 1399, 21:03 عصر
سلام من برای گزارش گیریم از این قطعه کد استفاده کرده ام


e.Graphics.DrawString((GridRow.Cells(int).Value.To String), GridRow.Cells(int).InheritedStyle.Font, _
New SolidBrush(GridRow.Cells(int).InheritedStyle.ForeC olor), _
New RectangleF(ColumnLefts(i), tmpTop, ColumnWidths(i), _
CellHeight), StrFormat)

حالا اگه بخوابم فرمت عددم رو به سه رقم جدا تبدیل کنم چیکار باید کنمم؟ (GridRow.Cells(int).Value.ToString
نمیدونم کد کامل تون چیه، ممکنه اینکار رو انجام داده باشید، ولی صرفا محض یادآوری، اشیاء ای که خودتون ایجاد می کنید و Dispose شدنی هستند رو حتما Dispose کنید.
هر زمان دیگه به شیء اش نیازی نداشتید وقت مناسب Dispose کردنش رسیده.
منظورم اون SolidBrush ئه. یا مستقیما بعد استفاده اش متد Dispose اون SolidBrush رو فراخوانی کنید یا به روش اصولی و بهتر از Using استفاده کنید تا بصورت خودکار و تضمینی Dispose بشه.
اگر تعداد رقم بعد اعشار براتون مهم نیست از "N" استفاده کنید، اگر ارقام بعد اعشار باید گرد و حذف شوند از "N0" و اگر قراره تا دو رقم بعد اعشار نمایش داده بشه "N2" و ...

Using brush As New SolidBrush( GridRow.Cells(int).InheritedStyle.ForeColor )
e.Graphics.DrawString( GridRow.Cells(int).Value.ToString("N") ) _
, GridRow.Cells(int).InheritedStyle.Font, brush _
, New RectangleF( ColumnLefts(i), tmpTop, ColumnWidths(i), CellHeight), StrFormat)
End Using

فرض رو بر این گرفته ایم که GridRow.Cells(int).Value یک عدد ئه. اگر مطمئن نیستید که مقدار داخل GridRow.Cells(int).Value عدد ئه، مثلا احتمال داره String باشه و لذا ممکنه که فرمت "N" برای اون نوع داده معتبر نباشه، بهتره قبلش به Double تبدیلش کنیم تا جای بروز مشکل نباشه :

Using brush As New SolidBrush( GridRow.Cells(int).InheritedStyle.ForeColor )
Dim n = Convert.ToDouble( GridRow.Cells(int).Value )
e.Graphics.DrawString( n.ToString("N") _
, GridRow.Cells(int).InheritedStyle.Font, brush _
, New RectangleF(ColumnLefts(i), tmpTop, ColumnWidths(i), CellHeight), StrFormat)
End Using