Link to home
Start Free TrialLog in
Avatar of mathieu_cupryk
mathieu_cuprykFlag for Canada

asked on

I need one decimal place.

format the string so the output will always have 1 decimal

sw.Write(dt.Rows[row].ItemArray[3].ToString()
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Try:
sw.Write( System.Math.Round(dt.Rows[row].ItemArray[3].ToString(),1) )
hi mathieu, you can exploit ToString() method in a better way:
sw.Write(dt.Rows[row].ItemArray[3].ToString("N1")
Avatar of mathieu_cupryk

ASKER

c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll: (Related file)
C:\SvnWork\InitialPriceReporting\DotNet\InitialPriceReporting\clsGenerateReadyToLoad.cs(89,33): error CS1501: No overload for method 'ToString' takes '1' arguments

sw.Write(dt.Rows[row].ItemArray[3].ToString("N1")

Also guys no rounding is required.
i tried this
String.Format("{0:0.0}", Convert.ToDouble(dt.Rows[row].ItemArray[3].ToString())
did not work either.
I see, you need a previous cast before invoking ToString:

sw.Write(   ((double)dt.Rows[row].ItemArray[3]).ToString("N1")  );

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\InitialPriceReporting\DotNet\InitialPriceReporting\clsGenerateReadyToLoad.cs(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.
what type is itemarray[3] in your database?
try with:

sw.Write(   double.Parse(dt.Rows[row].ItemArray[3]).ToString("N1")  );

Sorry. it should be:
sw.Write(   double.Parse(dt.Rows[row].ItemArray[3].ToString()).ToString("N1")  );
it says imput string in correct format.
could you post what appears in:dt.Rows[row].ItemArray[3].ToString() ?
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
well, I cannot see the object type but appears it is double, so this should work:

sw.Write(   ((double)dt.Rows[row].ItemArray[3]).ToString("N1")  );

at least try to evaluate first:

double x = (double)dt.Rows[row].ItemArray[3];

then x.ToString("N1") should work smoothly

how should i evaluate this?
it tells me specified cast not valid
I am really confused with this, you can try to test as:

MessageBox.Show(dt.Rows[row].ItemArray[3].GetType().ToString());
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].ItemArray[3])).ToString("N1")  );


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].ItemArray[3]);

Still not working.:-(
 // Protein Grade 6
                       decimal x = ((decimal)(dt.Rows[row].ItemArray[3]));

                        // MessageBox.Show(dt.Rows[row].ItemArray[3].GetType().ToString());
                       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() ) )
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.
String.IsNullOrEmpty(value)
Still not doing one decimal place.
 if ((dt.Rows[row].ItemArray[3].ToString() != null) && (!String.IsNullOrEmpty(dt.Rows[row].ItemArray[3].ToString())))
                       {
                           sw.Write(String.Format("{0:0.0}", Decimal.Parse(dt.Rows[row].ItemArray[3].ToString())));

                       }
                       else
                         sw.Write("");
ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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());

Open in new window

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