📜  在C#中使用String.Format()方法添加带浮点数的文本

📅  最后修改于: 2021-05-29 15:30:44             🧑  作者: Mango

这里的任务是使用String.Format()方法添加带有浮点数F的文本T。

例子 :

可以使用String.Format()方法通过以下方式将文本T添加到浮点数F:

  • 仅在浮点数F的整数部分的左侧添加文本T。
  • 仅在浮点数F的整数部分的右边添加文本T。
  • 仅在浮点数F的小数部分的左边添加文本T。
  • 仅在浮点数F的小数部分的右边添加文本T。
  • 在浮点数F小数点两边添加文本T。
  • 在浮点数F的两侧添加文本T。

下面是上述不同方式的实现:

示例1:仅在浮点数F的整数部分的左侧添加文本T。

C#
// C# program to add text T 
// only to left of integral 
// part of a float number F.
  
using System;
  
public class GFG{
      
    // function to add text at
    // left of integral part 
    // of a float number.
    static string Add_text(float F, string T)
    {
        // string format
        string s = "{0:";
        s += T;
        s += "0.0}";
          
        // use of String.Format() method
        return String.Format(s, F);
    }
      
    // Main Method
    static void Main(string[] args)
    {
        float F = 12.3f;
        string T = "abc";
          
        //function calling
        string str = Add_text(F, T);
          
        // print the added text float number
        Console.WriteLine(str);
    }
}


C#
// C# program to add text T 
// only to right of integral 
// part of a float number F.
  
using System;
  
public class GFG{
      
    // function to add text at
    // right of integral part 
    // of a float number.
    static string Add_text(float F, string T)
    {
        // string format
        string s = "{0:0";
        s += T;
        s += ".0}";
          
        // use of String.Format() method
        return String.Format(s, F);
    }
      
    // Main Method
    static void Main(string[] args)
    {
        float F = 12.3f;
        string T = "abc";
          
        // function calling
        string str = Add_text(F, T);
          
        // print the added text float number
        Console.WriteLine(str);
    }
}


C#
// C# program to add text T 
// only to left of fractional 
// part of a float number F.
  
using System;
  
public class GFG{
      
    // function to add text at
    // left of fractional part 
    // of a float number F.
    static string Add_text(float F, string T)
    {
        // string format
        string s = "{0:0.";
        s += T;
        s += "0}";
          
        // use of String.Format() method
        return String.Format(s, F);
    }
      
    // Main Method
    static void Main(string[] args)
    {
        float F = 12.3f;
        string T = "abc";
          
        // function calling
        string str = Add_text(F, T);
          
        // print the added text
        // float number
        Console.WriteLine(str);
    }
}


C#
// C# program to add text T 
// only to right of fractional 
// part of a float number F.
  
using System;
  
public class GFG{
      
    // function to add text at
    // right of fractional part 
    // of a float number F.
    static string Add_text(float F, string T)
    {
        // string format
        string s = "{0:0.0";
        s += T;
        s += "}";
          
        // use of String.Format() method
        return String.Format(s, F);
    }
      
    // Main Method
    static void Main(string[] args)
    {
        float F = 12.3f;
        string T = "abc";
          
        // function calling
        string str = Add_text(F, T);
          
        // print the added text float number
        Console.WriteLine(str);
    }
}


C#
// C# program to add text
// to both side of decimal
// point of a float number
  
using System;
  
public class GFG{
      
    // function to add text at
    // both side of decimal
    // point of a float number
    static string Add_text(float F, string T)
    {
        // string format
        string s = "{0:0";
        s += T;
        s += ".";
        s += T;
        s += "0}";
          
        // use of String.Format() method
        return String.Format(s, F);
    }
      
    // Main Method
    static void Main(string[] args)
    {
        float F = 12.3f;
        string T = "abc";
          
        // function calling
        string str = Add_text(F, T);
          
        // print the added text float number
        Console.WriteLine(str);
    }
}


C#
// C# program to add text
// to both side of a float number
  
using System;
  
public class GFG{
      
    // function to add text at
    // both side of a float number
    static string Add_text(float F, string T)
    {
        // string format
        string s = "{0:";
        s += T;
        s += "0.0";
        s += T;
        s += "}";
          
        // use of String.Format() method
        return String.Format(s, F);
    }
      
    // Main Method
    static void Main(string[] args)
    {
        float F = 12.3f;
        string T = "abc";
          
        // function calling
        string str = Add_text(F, T);
          
        // print the added text float number
        Console.WriteLine(str);
    }
}


输出:

abc12.3

示例2:仅在浮点数F的整数部分的右边添加文本T。

C#

// C# program to add text T 
// only to right of integral 
// part of a float number F.
  
using System;
  
public class GFG{
      
    // function to add text at
    // right of integral part 
    // of a float number.
    static string Add_text(float F, string T)
    {
        // string format
        string s = "{0:0";
        s += T;
        s += ".0}";
          
        // use of String.Format() method
        return String.Format(s, F);
    }
      
    // Main Method
    static void Main(string[] args)
    {
        float F = 12.3f;
        string T = "abc";
          
        // function calling
        string str = Add_text(F, T);
          
        // print the added text float number
        Console.WriteLine(str);
    }
}

输出:

12abc.3

示例3:仅将文本T添加到浮点数F的小数部分的左侧。

C#

// C# program to add text T 
// only to left of fractional 
// part of a float number F.
  
using System;
  
public class GFG{
      
    // function to add text at
    // left of fractional part 
    // of a float number F.
    static string Add_text(float F, string T)
    {
        // string format
        string s = "{0:0.";
        s += T;
        s += "0}";
          
        // use of String.Format() method
        return String.Format(s, F);
    }
      
    // Main Method
    static void Main(string[] args)
    {
        float F = 12.3f;
        string T = "abc";
          
        // function calling
        string str = Add_text(F, T);
          
        // print the added text
        // float number
        Console.WriteLine(str);
    }
}

输出:

12.abc3

示例4:仅在浮点数F的小数部分的右边添加文本T。

C#

// C# program to add text T 
// only to right of fractional 
// part of a float number F.
  
using System;
  
public class GFG{
      
    // function to add text at
    // right of fractional part 
    // of a float number F.
    static string Add_text(float F, string T)
    {
        // string format
        string s = "{0:0.0";
        s += T;
        s += "}";
          
        // use of String.Format() method
        return String.Format(s, F);
    }
      
    // Main Method
    static void Main(string[] args)
    {
        float F = 12.3f;
        string T = "abc";
          
        // function calling
        string str = Add_text(F, T);
          
        // print the added text float number
        Console.WriteLine(str);
    }
}

输出:

12.3abc

示例5:将文本T添加到浮点数F的小数点两侧。

C#

// C# program to add text
// to both side of decimal
// point of a float number
  
using System;
  
public class GFG{
      
    // function to add text at
    // both side of decimal
    // point of a float number
    static string Add_text(float F, string T)
    {
        // string format
        string s = "{0:0";
        s += T;
        s += ".";
        s += T;
        s += "0}";
          
        // use of String.Format() method
        return String.Format(s, F);
    }
      
    // Main Method
    static void Main(string[] args)
    {
        float F = 12.3f;
        string T = "abc";
          
        // function calling
        string str = Add_text(F, T);
          
        // print the added text float number
        Console.WriteLine(str);
    }
}

输出:

12abc.abc3

示例6:在浮点数F的两侧添加文本T。

C#

// C# program to add text
// to both side of a float number
  
using System;
  
public class GFG{
      
    // function to add text at
    // both side of a float number
    static string Add_text(float F, string T)
    {
        // string format
        string s = "{0:";
        s += T;
        s += "0.0";
        s += T;
        s += "}";
          
        // use of String.Format() method
        return String.Format(s, F);
    }
      
    // Main Method
    static void Main(string[] args)
    {
        float F = 12.3f;
        string T = "abc";
          
        // function calling
        string str = Add_text(F, T);
          
        // print the added text float number
        Console.WriteLine(str);
    }
}

输出:

abc12.3abc