Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Convert decimal to string with specify format

I need to convert decimal number a to string b folowing:

  • b must be haven '.' character. Eg:
    • a = 12 -> b = "12.0"
    • a = 1.2 -> b = "1.2"
    • a = 1.234 -> b = "1.234"

How can I do that with 1 command?

  • b must be haven exactly 10 character. Eg:
    • a = 101 -> b = "101.000000"
    • a = 1.234 -> b = "1.23400000"
    • a = 1.234567891 -> b = "1.23456789"

(Same question with 1)

like image 652
TamHoang Avatar asked Aug 12 '16 06:08

TamHoang


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 C full form?

Originally Answered: What is the full form of C ? C - Compiler . C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972.

How old is the letter C?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


2 Answers

decimal a = 12;
var b = a.ToString("N1"); // 12.0

a = 1.2m;
b = a.ToString(); // 1.2

a = 101m;
b = a.ToString("N10"); // 101.0000000000

a = 1.234m;
b = a.ToString("N10"); // 1.2340000000

For the second part of your question - where you want a total length of 10 then:

decimal a = 1.234567891m;
int numberOfDigits = ((int)a).ToString().Length;
var b = a.ToString($"N{9 - numberOfDigits}"); //1.23456789

//Or before C# 6.0
var b = a.ToString("N" + (9 - numberOfDigits)); //1.23456789

Basically ((int)number).ToString().Length gives you the amount of digits before the . (converting to int will remove the fractions) and then reducing that from the number of digits after the . (and also -1 for the decimal point itself)

like image 114
Gilad Green Avatar answered Sep 20 '22 22:09

Gilad Green


You can use .ToString() to do this task:

decimal aDecimalVal = 10.234m;
string decimalString = aDecimalVal.ToString("#.000"); // "10.234"
aDecimalVal.ToString("#.00"); // "10.23"
aDecimalVal.ToString("#.0000"); // "10.2340"

The number of 0 after the . in the format string will decide the number of places in the decimal string.

Updates: So you want to find the number of digits after the decimal points, So the changes in the code will be like the following:

decimal aDecimalVal = 10.2343455m;
int count = BitConverter.GetBytes(decimal.GetBits(aDecimalVal)[3])[2];
string formatString = String.Format("N{0}",count.ToString());
string decimalString = aDecimalVal.ToString(formatString); // "10.2343455"
like image 36
sujith karivelil Avatar answered Sep 16 '22 22:09

sujith karivelil