Last active
January 5, 2021 10:38
-
-
Save nesendal/c781f6b6211003bcac8d1026be81468e to your computer and use it in GitHub Desktop.
C# Virgüllü Sayıyı Yuvarlamadan Virgülden Sonra İki Basamak Alma
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace virgullusayiOrnek | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine(Formatla(113.129)); | |
Console.WriteLine(Formatla(113)); | |
Console.WriteLine(Formatla(13.99999)); | |
//nagihanesendal.blogspot.com | |
} | |
public static string Formatla(double sayi) | |
{ | |
string neww = string.Empty; | |
int virgulindex = sayi.ToString().IndexOf('.'); | |
if (virgulindex > 0) | |
{ | |
int virgulsonrasiBasamakAdedi = sayi.ToString().Length - 1 - virgulindex; | |
neww += sayi.ToString().Substring(0, virgulindex); | |
neww += "."; | |
if (virgulsonrasiBasamakAdedi > 2) | |
{ | |
neww += sayi.ToString().Substring(virgulindex + 1, 2); | |
} | |
else | |
{ | |
neww += sayi.ToString().Substring(virgulindex + 1, virgulsonrasiBasamakAdedi); | |
} | |
return neww; | |
} | |
else | |
{ | |
return sayi.ToString(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment