Here the task is to add the text T with a float number F using String.Format() method.
Example :
Input : F = 12.3, T = “abc”
Output : 12abc.abc3Input : F = 0.0, T = “Geeks”
Output : Geeks0.0Geeks
The text T can be add to a float number F using String.Format() method in a following way:
- Add text T only to left of integral part of a float number F.
- Add text T only to right of integral part of a float number F.
- Add text T only to left of fractional part of a float number F.
- Add text T only to right of fractional part of a float number F.
- Add text T to both side of decimal point of a float number F.
- Add text T to both side of a float number F.
Below is the implementation of the above different ways:
Example 1: Adding text T only to left of integral part of a float number F.
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); } } |
Output:
abc12.3
Example 2: Adding text T only to right of integral part of a float number F.
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); } } |
Output:
12abc.3
Example 3: Adding text T only to left of fractional part of a float number 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); } } |
Output:
12.abc3
Example 4: Adding text T only to right of fractional part of a float number F.
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); } } |
Output:
12.3abc
Example 5: Adding text T to both side of decimal point of a float number 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); } } |
Output:
12abc.abc3
Example 6: Adding text T to both side of a float number F.
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); } } |
Output:
abc12.3abc