Skip to content

Instantly share code, notes, and snippets.

@nesendal
Last active January 5, 2021 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nesendal/c781f6b6211003bcac8d1026be81468e to your computer and use it in GitHub Desktop.
Save nesendal/c781f6b6211003bcac8d1026be81468e to your computer and use it in GitHub Desktop.
C# Virgüllü Sayıyı Yuvarlamadan Virgülden Sonra İki Basamak Alma
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