-3

Example:*

string str = "i have rs 12.55"

and I want to print this as

"i have rs 12"

while ignoring .55 in sentence.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215

2 Answers2

0

You can try with string function Substring and capture the string till .

string str = "i have rs 12.55";
var result = str.Substring(0, str.IndexOf('.'));

However, I suggest to remove the decimal part from 12.55 before forming the string.

double value = 12.55;
string str = $"i have rs {(int)value}";

OR

decimal value = 12.55M;
string str = $"i have rs {decimal.Truncate(value)}";
Krishna Varma
  • 4,238
  • 2
  • 10
  • 25
0

In order to remove all fractional parts, you can try regular expressions:

using System.Text.RegularExpressions;

... 

Regex regex = new Regex(@"(?<=[0-9]+)\.[0-9]+");

string result = regex.Replace(str, "");

Demo:

  string[] tests = new string[] {
    "i have rs 12.55",
    "I have rs 12.55 and -8.63 but 0.78963",
    "list : A.B.C.D",
    "12 - 45... but 78.99"
  };

  Regex regex = new Regex(@"(?<=[0-9]+)\.[0-9]+");

  string report = string.Join(Environment.NewLine, tests
    .Select(test => $"{test,-40} => {regex.Replace(test, "")}")) ;

  Console.Write(report);

Outcome:

i have rs 12.55                          => i have rs 12
I have rs 12.55 and -8.63 but 0.78963    => I have rs 12 and -8 but 0
list : A.B.C.D                           => list : A.B.C.D
12 - 45... but 78.99                     => 12 - 45... but 78
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215