Adding Text with a Float number Using String.Format() Method in C#
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.abc3
Input : 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#
using System;
public class GFG{
static string Add_text( float F, string T)
{
string s = "{0:" ;
s += T;
s += "0.0}" ;
return String.Format(s, F);
}
static void Main( string [] args)
{
float F = 12.3f;
string T = "abc" ;
string str = Add_text(F, T);
Console.WriteLine(str);
}
}
|
Output:
abc12.3
Example 2: Adding text T only to right of integral part of a float number F.
C#
using System;
public class GFG{
static string Add_text( float F, string T)
{
string s = "{0:0" ;
s += T;
s += ".0}" ;
return String.Format(s, F);
}
static void Main( string [] args)
{
float F = 12.3f;
string T = "abc" ;
string str = Add_text(F, T);
Console.WriteLine(str);
}
}
|
Output:
12abc.3
Example 3: Adding text T only to left of fractional part of a float number F.
C#
using System;
public class GFG{
static string Add_text( float F, string T)
{
string s = "{0:0." ;
s += T;
s += "0}" ;
return String.Format(s, F);
}
static void Main( string [] args)
{
float F = 12.3f;
string T = "abc" ;
string str = Add_text(F, T);
Console.WriteLine(str);
}
}
|
Output:
12.abc3
Example 4: Adding text T only to right of fractional part of a float number F.
C#
using System;
public class GFG{
static string Add_text( float F, string T)
{
string s = "{0:0.0" ;
s += T;
s += "}" ;
return String.Format(s, F);
}
static void Main( string [] args)
{
float F = 12.3f;
string T = "abc" ;
string str = Add_text(F, T);
Console.WriteLine(str);
}
}
|
Output:
12.3abc
Example 5: Adding text T to both side of decimal point of a float number F.
C#
using System;
public class GFG{
static string Add_text( float F, string T)
{
string s = "{0:0" ;
s += T;
s += "." ;
s += T;
s += "0}" ;
return String.Format(s, F);
}
static void Main( string [] args)
{
float F = 12.3f;
string T = "abc" ;
string str = Add_text(F, T);
Console.WriteLine(str);
}
}
|
Output:
12abc.abc3
Example 6: Adding text T to both side of a float number F.
C#
using System;
public class GFG{
static string Add_text( float F, string T)
{
string s = "{0:" ;
s += T;
s += "0.0" ;
s += T;
s += "}" ;
return String.Format(s, F);
}
static void Main( string [] args)
{
float F = 12.3f;
string T = "abc" ;
string str = Add_text(F, T);
Console.WriteLine(str);
}
}
|
Output:
abc12.3abc
Last Updated :
26 May, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...