且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

修剪和删除字符串中的数字

更新时间:2023-02-06 11:00:24

请阅读:

标准数字格式字符串| Microsoft Docs [ ^ ]

自定义数字格式字符串| Microsoft Docs [ ^ ]



获取数值的一种正确方法是使用Double.Parse方法将字符串转换为double,然后显示它通过使用ToString()方法。例如:



  string  stramount =   000000099; 
double dblamount = Double .Parse(stramount,System.Globalization.NumberStyles.AllowDecimalPoint,System .Globalization.CultureInfo.InvariantCulture);
dblamount / = 100 ;
Console.WriteLine(dblamount.ToString( 0.00)); // 显示0.99





祝你好运!


我理解你的要求是这样的:

- 你总是有一个只包含数字的字符串。

- 你总是希望将字符串的最后2个数字作为小数。



所以你的代码看起来像这样:
  string  s =   000000099; 
int i =( int )s;
double d =( double )i / 100 跨度>;
string s2 = d.ToString( 0.00\" 跨度>);



我已经分步完成了,所以你可以看看它是如何工作的。

代码本身可以被压缩...


Actually i have the value "000000099"
and after some trimming methods the value finally get displayed as ".99"
Here actually trimming the front all zero's and displaying from DOT(front all 0's are removed or trimmed) i.e; finally it displays as ".99"

So here i need, if the value less than 1 like "0.99" or "0.73" or "0.35"
it should get displayed as "0.99 or 0.73"
but not like ".99 or .73"
Please Help me out with this friends...........!!

What I have tried:

public string FormateStringAmount( string strValue ) // here string value is getting like 0000000099 for 0.99 value
{
try
{
if (strValue.Trim () =="" ) return "0.00" ;

string strTrimString = strValue.Trim(); 		
string strAmount  = "0.00";
int intLength =strTrimString.Length ;

if ( strTrimString.EndsWith("+")  )
  {
    strAmount =  strTrimString.Substring(0,intLength -3)+"." + 
    strTrimString.Substring( intLength -3,2) ;
  }
else if (  strTrimString.EndsWith("-"))
  {
    strAmount =  strTrimString.Substring(0,intLength -3)+"." + 
    strTrimString.Substring( intLength -3,2)+"-" ;
  }
else
  {
    strAmount = strTrimString.Substring(0,intLength -2)+"." + 
    strTrimString.Substring( intLength -2,2);
  }

  strAmount = strAmount.TrimStart ('0');// here the value is getting trimmed front Zero's
   if (strAmount.Equals (".00") || strAmount.Equals (".00-"))  
   strAmount ="0.00" ;
   return strAmount;
    }
	Catch (Exception)
	{	 
	return strValue;
	}
}

Please, read this:
Standard Numeric Format Strings | Microsoft Docs[^]
Custom Numeric Format Strings | Microsoft Docs[^]

A proper way to get numeric value is to convert string into double by using Double.Parse method, then to display it by using ToString() method. For example:

string stramount = "000000099";
double dblamount = Double.Parse(stramount, System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.InvariantCulture);
dblamount /= 100;
Console.WriteLine(dblamount.ToString("0.00")); //displays 0.99



Good luck!


I understand your requirement like this :
- You have allways a string which only contains numbers.
- You allways want to have the last 2 numbers of the string as decimals.

So your code could look like this :
string s = "000000099";
int i = (int)s;
double d = (double)i / 100;
string s2 = d.ToString("0.00");


I have done it in steps so you could see how it works.
The code itself could be compressed ...