Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# String format: escape a dot

I have a line of code, something like:

mbar.HealthLabel.text = String.Format("{0:0.0}", _hp);

Output is: 2.25 for example. Is it possible to escape a dot from the output string view with String.Format function ?

For. ex. 225,

To make my question more clear, I need the same effect like:

Math.Floor(_hp * 100).ToString();

But need to do it by String.Format template.. Thanks.

like image 399
Maximilian Avatar asked May 05 '17 10:05

Maximilian


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

Simply you can do it this way

double value = 1.25;

var stringValue = string.Format("{0:0}", value * 100, CultureInfo.InvariantCulture); //125

EDIT: More general solution would be to Replace the dot with empty string as stated in the comments.

double value = 1.25;

var stringValue = value.ToString(CultureInfo.InvariantCulture).Replace(".",string.Empty);

EDIT2: Also there is another general idea that do not use Replace function (but also it does not use the String.Format)

var stringValue = string.Join("", value.ToString().Where(char.IsDigit));

Also another similar idea:

var stringValue = new string(value.ToString().Where(char.IsDigit).ToArray());
like image 164
Hossein Narimani Rad Avatar answered Sep 30 '22 01:09

Hossein Narimani Rad