mathieu_cupryk
asked on
I need one decimal place.
format the string so the output will always have 1 decimal
sw.Write(dt.Rows[row].Item Array[3].T oString()
sw.Write(dt.Rows[row].Item
hi mathieu, you can exploit ToString() method in a better way:
sw.Write(dt.Rows[row].Item Array[3].T oString("N 1")
sw.Write(dt.Rows[row].Item
ASKER
c:\WINDOWS\Microsoft.NET\F ramework\v 2.0.50727\ mscorlib.d ll: (Related file)
C:\SvnWork\InitialPriceRep orting\Dot Net\Initia lPriceRepo rting\clsG enerateRea dyToLoad.c s(89,33): error CS1501: No overload for method 'ToString' takes '1' arguments
sw.Write(dt.Rows[row].Item Array[3].T oString("N 1")
C:\SvnWork\InitialPriceRep
sw.Write(dt.Rows[row].Item
ASKER
Also guys no rounding is required.
ASKER
i tried this
String.Format("{0:0.0}", Convert.ToDouble(dt.Rows[r ow].ItemAr ray[3].ToS tring())
did not work either.
String.Format("{0:0.0}", Convert.ToDouble(dt.Rows[r
did not work either.
I see, you need a previous cast before invoking ToString:
sw.Write( ((double)dt.Rows[row].Item Array[3]). ToString(" N1") );
sw.Write( ((double)dt.Rows[row].Item
ASKER
Your way gives me specific cast not valid then I tried this way.
All I need is a string.
so lets take out the stupid cast.
C:\SvnWork\InitialPriceRep orting\Dot Net\Initia lPriceRepo rting\clsG enerateRea dyToLoad.c s(84,48): error CS1501: No overload for method 'ToString' takes '1' arguments
Itried this way
sw.Write((Decimal.Parse( dt.Rows[row].ItemArray[3]. ToString(" N1") ) ) );
still gives me an error.
All I need is a string.
so lets take out the stupid cast.
C:\SvnWork\InitialPriceRep
Itried this way
sw.Write((Decimal.Parse( dt.Rows[row].ItemArray[3].
still gives me an error.
what type is itemarray[3] in your database?
try with:
sw.Write( double.Parse(dt.Rows[row]. ItemArray[ 3]).ToStri ng("N1") );
try with:
sw.Write( double.Parse(dt.Rows[row].
Sorry. it should be:
sw.Write( double.Parse(dt.Rows[row]. ItemArray[ 3].ToStrin g()).ToStr ing("N1") );
sw.Write( double.Parse(dt.Rows[row].
ASKER
it says imput string in correct format.
could you post what appears in:dt.Rows[row].ItemArray[ 3].ToStrin g() ?
ASKER
Sure see attached file.
dt.Rows[row].ItemArray[3]. ToString() "11" string
and in the array see attached file.
7-19-2008-9-52-28-AM.gif
dt.Rows[row].ItemArray[3].
and in the array see attached file.
7-19-2008-9-52-28-AM.gif
well, I cannot see the object type but appears it is double, so this should work:
sw.Write( ((double)dt.Rows[row].Item Array[3]). ToString(" N1") );
at least try to evaluate first:
double x = (double)dt.Rows[row].ItemA rray[3];
then x.ToString("N1") should work smoothly
sw.Write( ((double)dt.Rows[row].Item
at least try to evaluate first:
double x = (double)dt.Rows[row].ItemA
then x.ToString("N1") should work smoothly
ASKER
how should i evaluate this?
ASKER
it tells me specified cast not valid
I am really confused with this, you can try to test as:
MessageBox.Show(dt.Rows[ro w].ItemArr ay[3].GetT ype().ToSt ring());
MessageBox.Show(dt.Rows[ro
ASKER
It says system.decimal.
So, I don't know how cannot be casted to double. Anyway, try to cast to decimal before calling ToString():
w.Write( ((decimal)(dt.Rows[row].It emArray[3] )).ToStrin g("N1") );
w.Write( ((decimal)(dt.Rows[row].It
ASKER
it is says specified cast invalid. remember i am writting out to a csv file.
if dt.Rows[row].ItemArray[3] is of type decimal, then casting cannot be invalid:
decimal x = ((decimal)(dt.Rows[row].It emArray[3] );
decimal x = ((decimal)(dt.Rows[row].It
ASKER
Still not working.:-(
// Protein Grade 6
decimal x = ((decimal)(dt.Rows[row].It emArray[3] ));
// MessageBox.Show(dt.Rows[ro w].ItemArr ay[3].GetT ype().ToSt ring());
sw.Write(x.ToString("N1")) ;
sw.Write(",");
7-19-2008-11-06-50-AM.gif
// Protein Grade 6
decimal x = ((decimal)(dt.Rows[row].It
// MessageBox.Show(dt.Rows[ro
sw.Write(x.ToString("N1"))
sw.Write(",");
7-19-2008-11-06-50-AM.gif
Try:
sw.Write( int.Parse( dt.Rows[row].ItemArray[3]. ToString() ) )
sw.Write( int.Parse( dt.Rows[row].ItemArray[3].
ASKER
ASKER
PRoblem is this
if ((dt.Rows[row].ItemArray[3 ].ToString () != null) || (dt.Rows[row].ItemArray[3] .ToString( ) != ""))
It is emtpy.
What should I compare to it still goes in the if.
if ((dt.Rows[row].ItemArray[3
It is emtpy.
What should I compare to it still goes in the if.
ASKER
String.IsNullOrEmpty(value )
ASKER
Still not doing one decimal place.
if ((dt.Rows[row].ItemArray[3 ].ToString () != null) && (!String.IsNullOrEmpty(dt. Rows[row]. ItemArray[ 3].ToStrin g())))
{
sw.Write(String.Format("{0 :0.0}", Decimal.Parse(dt.Rows[row] .ItemArray [3].ToStri ng())));
}
else
sw.Write("");
if ((dt.Rows[row].ItemArray[3
{
sw.Write(String.Format("{0
}
else
sw.Write("");
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If nothing else works, try this:
decimal dec = (decimal)dt.Rows[row].ItemArray[3];
int temp = (int)(dec * 10);
dec = (decimal)(temp / 10.0);
sw.Write(dec.ToString());
ASKER
did not work. :-(
Um it should have.
Maybe change line 3 to this:
dec = ((decimal)temp / 10.0);
578.934 // original dec
5789 // temp
578.9 // new dec
Maybe change line 3 to this:
dec = ((decimal)temp / 10.0);
578.934 // original dec
5789 // temp
578.9 // new dec
sw.Write( System.Math.Round(dt.Rows[