SoFunction
Updated on 2025-03-07

Calculation of decimals and percentages in C# and byte array operations

1. Percentage

//According to the international standard of roundingstring p1 = ((0.333367, 4)*100).ToString() + "%";//33.34%
double dbdata = ((double)1 / (double)3, 5) * 100;//33.333
string p2 = ("{0:F}", dbdata) + "%";//The default is to keep two digits

2. Category retention

With decimal points

//Please guarantee that the denominator is doubledouble t = (1.0 / 3, 5) * 100;//33.33
double tt = (1.0 / 3.0, 5) * 100;//33.333
//m stands for decimal.decimal res = 100m / 1000;//0.1

No decimal points

//Reserve as an integerdouble ttt = (1.0 / 3.0, 0);//0

3. Take the remainder

int a = 10 % 3;//Take the remainder

byte array operation

//1. Byte conversionfloat m = 5f;
var btValue = (m).Reverse().ToArray();
//Convert to original value stringstring m1 = (btValue);

// Combination andbyte[] data = new byte[10];
byte[] counts = new byte[3];
byte[] ndata = new byte[ + ];
//Copy data to ndata(ndata, 0);//Storage from where the subscript of ndata is 0(ndata, );

//Convert with byte[]string str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// string to byte[]:byte[] byteArray1 = (str);
//byte[] to string:string str1 = (byteArray1);
// string to ASCII byte[]:byte[] byteArray2 = (str);
//ASCII byte[] to string:string str2 = (byteArray2);

//4. String split arraystring a = "A|B|C|D";
string[] a1 = ('|');

//Convert to hexadecimalint b = 58;
byte b1 = (b);

//Array interceptbyte[] test = (4).Take(3).ToArray();//From subscript 4, length 3
//Convert to Byte[]List<byte> frameBytes = new List<byte>();
(0x9E);
byte[] phoneNumByte = new byte[] { 0x01, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00 };//Define an arrayfor (int i = 0; i < ; i++)
{
    (phoneNumByte[i]);
}
frameBytes = (byteArray2).ToList<byte>();//Merge two lists//list to byte[]byte[] transByte = ();
//byte[] to listList<byte> lb =();

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.