Click here to Skip to main content
15,885,842 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have the data in my code like below which I need to format differently(C# code).


3.99E+03    5.3082E+03  4.719E+03   8.001E-01   7.3568E+02  5.759E+02   1.9892E+03  
3.99E+03    6.0085E+03  4.5499E+03  8.321E-01   7.3305E+02  5.749E+02   2.0866E+03  
3.99E+03    6.7089E+03  4.3104E+03  8.53E-01    7.2935E+02  5.738E+02   2.1531E+03  
3.99E+03    7.4092E+03  4.0095E+03  8.625E-01   7.2471E+02  5.727E+02   2.1877E+03  
3.99E+03    8.1096E+03  3.6563E+03  8.602E-01   7.1929E+02  5.716E+02   2.1892E+03  
3.99E+03    8.8099E+03  3.2602E+03  8.461E-01   7.1325E+02  5.704E+02   2.156E+03   
3.99E+03    9.5103E+03  2.8306E+03  8.212E-01   7.0675E+02  5.692E+02   2.082E+03   
3.99E+03    1.0211E+04  2.3637E+03  7.848E-01   6.9973E+02  5.68E+02    1.9544E+03


Formatted data should look like below:

3.99E+03 

5.3082E+03  4.719E+03   8.001E-01   7.3568E+02  5.759E+02   1.9892E+03  
6.0085E+03  4.5499E+03  8.321E-01   7.3305E+02  5.749E+02   2.0866E+03  
6.7089E+03  4.3104E+03  8.53E-01    7.2935E+02  5.738E+02   2.1531E+03  
7.4092E+03  4.0095E+03  8.625E-01   7.2471E+02  5.727E+02   2.1877E+03  
8.1096E+03  3.6563E+03  8.602E-01   7.1929E+02  5.716E+02   2.1892E+03  
8.8099E+03  3.2602E+03  8.461E-01   7.1325E+02  5.704E+02   2.156E+03   
9.5103E+03  2.8306E+03  8.212E-01   7.0675E+02  5.692E+02   2.082E+03   
1.0211E+04  2.3637E+03  7.848E-01   6.9973E+02  5.68E+02    1.9544E+03


What I have tried:

Here is the piece of code I am trying.

var sb = new StringBuilder();
var csvSb = new StringBuilder();
string result = string.Empty;

var rpmvalue = dto.FirstOrDefault()?.data.FirstOrDefault();


for (int i = 0; i < dto[0].data.Count; i++)
{
for (int j = 1; j < dto.Count; j++)
{
result = dto[j].data.Count > 0 && dto[j].data[i] != 0 ? Convert.ToString(dto[j].data[i]) : string.Empty;
double dresult = 0.00;
if (Double.TryParse(result, out double number))
{
dresult = number;
}
result = string.Format("{0:0.#####E+00}", dresult);
AppendPadded(sb, csvSb, result, OperatingParamsValueWidth);
}
sb.AppendLine();
csvSb.AppendLine();
}


public void AppendPadded(StringBuilder sb, StringBuilder csvSb, string s, int count)
{
if (null == s)
s = string.Empty;
int maxLength = 10;
sb.Append(s);
//if (count - s.Length > 0)
sb.Append(' ', maxLength - s.Length + 2);
csvSb.Append(s);
csvSb.Append(',');
}
Posted
Updated 16-Mar-21 22:34pm
v3
Comments
Richard MacCutchan 16-Mar-21 8:49am    
What is the problem?
ritesh.gupta5555 16-Mar-21 9:39am    
I am unable to group it.
Richard MacCutchan 16-Mar-21 9:43am    
Unable to group what? All you have shown us is a printout of some numbers. We have no idea where these are coming from or what form they are in.
ritesh.gupta5555 16-Mar-21 9:45am    
the first column has a set of numbers which are same so i want to group by them. The other 6 columns should be as it is.
CHill60 16-Mar-21 8:59am    
And what logic are you applying to do this? What are the criteria - is it the specific value, always the first on a "line", common values? What?
Also - how is this data stored in your code? List, Array, GridView?

1 solution

Seems, you want to group data by column#1...

Take a look here: c# - How do I group data in an ASP.NET MVC View? - Stack Overflow[^]
 
Share this answer
 
Comments
ritesh.gupta5555 17-Mar-21 2:58am    
Thanks for the reply.However i want to achieve the same thing as you mentioned in C# using List and for loops.
Maciej Los 17-Mar-21 3:09am    
Follow the link. There you'll find what you need.
ritesh.gupta5555 17-Mar-21 4:36am    
Hi Maciej, i have pasted the piece of code which i am trying. Can you please suggest what should be corrected here.
Maciej Los 17-Mar-21 4:51am    
Everything! Your code does NOT correspond to MVC...
ritesh.gupta5555 17-Mar-21 5:40am    
My bad! But can you please suggest how to tweak this piece of code in C#?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900