Avatar of mathieu_cupryk
mathieu_cupryk
Flag 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()
.NET ProgrammingC#

Avatar of undefined
Last Comment
Hendo1973

8/22/2022 - Mon
hielo

Try:
sw.Write( System.Math.Round(dt.Rows[row].ItemArray[3].ToString(),1) )
Jaime Olivares

hi mathieu, you can exploit ToString() method in a better way:
sw.Write(dt.Rows[row].ItemArray[3].ToString("N1")
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")

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
mathieu_cupryk

ASKER
Also guys no rounding is required.
mathieu_cupryk

ASKER
i tried this
String.Format("{0:0.0}", Convert.ToDouble(dt.Rows[row].ItemArray[3].ToString())
did not work either.
Jaime Olivares

I see, you need a previous cast before invoking ToString:

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

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
mathieu_cupryk

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\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.
Jaime Olivares

what type is itemarray[3] in your database?
try with:

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

Jaime Olivares

Sorry. it should be:
sw.Write(   double.Parse(dt.Rows[row].ItemArray[3].ToString()).ToString("N1")  );
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
mathieu_cupryk

ASKER
it says imput string in correct format.
Jaime Olivares

could you post what appears in:dt.Rows[row].ItemArray[3].ToString() ?
mathieu_cupryk

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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Jaime Olivares

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

mathieu_cupryk

ASKER
how should i evaluate this?
mathieu_cupryk

ASKER
it tells me specified cast not valid
Your help has saved me hundreds of hours of internet surfing.
fblack61
Jaime Olivares

I am really confused with this, you can try to test as:

MessageBox.Show(dt.Rows[row].ItemArray[3].GetType().ToString());
mathieu_cupryk

ASKER
It says system.decimal.
Jaime Olivares

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")  );


⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
mathieu_cupryk

ASKER
it is says specified cast invalid. remember i am writting out to a csv file.
Jaime Olivares

if dt.Rows[row].ItemArray[3] is of type decimal, then casting cannot be invalid:

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

mathieu_cupryk

ASKER
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
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
hielo

Try:
sw.Write( int.Parse( dt.Rows[row].ItemArray[3].ToString() ) )
mathieu_cupryk

ASKER
mathieu_cupryk

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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
mathieu_cupryk

ASKER
String.IsNullOrEmpty(value)
mathieu_cupryk

ASKER
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
Jaime Olivares

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Hendo1973

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

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
mathieu_cupryk

ASKER
did not work. :-(
Hendo1973

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