簡體   English   中英

強制小數位為 VB.NET 中的字符串

[英]Force decimal places to a string in VB.NET

如何給字符串類型的數字添加小數點后六位0的試探?

我嘗試了 PadRight 但不是我想要的結果,例如: 125 ===to===> 125,000000 14,5 ====to===> 14,500000等等..

VB.NET 中是否有 function 可以做到這一點,或者我需要拆分字符串然后組合它們?

我不完全確定你在問什么,但如果你想在字符串中添加六個 0,那么我會制作一個 function,如下所示:

private static string AddZeroes(string myString) 
{
  //Makes an array that is 6 elements higher than the original string
  string[] myStringArray = new string[myString.Length + 6];
  //Sets the array to have all elements of myString inside of it
  for(int i = 0; i < myString.Length; i++)
  {
    myStringArray[i] = Convert.ToString(myString[i]);
  }
  //Adds the six 0's to the array
  for(int i = 0; i < 6; i++)
  {
    myString[i + myString.Length] = "0";
  }
  //Sets myString to an empty string, then adds all the contents of the myStringArray to it
  myString = "";
  for(int i = 0; i < myStringArray.Length; i++)
  {
    myString += myStringArray[i];
  }
  //Returns the string that should now have six 0's added to the end
  return myString;
}

如果你想要一個逗號或小數,在添加 0 的 for 循環之前添加一個應該不會太難,並且還要確保數組是字符串的長度 + 7,以適應額外的特點。

抱歉,如果這不是您要找的答案,我只是想回答我認為您要問的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM