0
String num = "93456"

decimal decinum = Convert.ToDecimal(num);
var newnum = decinum.ToString();

I need to convert string value into decimal value and back to string value

For instance, 93456 string format in to 93.456 decimal format then back to 93.456 string format.

I need to place a decimal after two fist digits of a string.

Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49
user2178940
  • 15
  • 1
  • 1
  • 6

6 Answers6

2

Here's my attempt:

String myString = "93456";

if (myString.Length > 2)
{
    myString = myString.Insert(2, Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator);
}

var myDecimal = decimal.Parse(myString);

Console.WriteLine(myDecimal);
Console.WriteLine(myDecimal.ToString());
tymtam
  • 31,798
  • 8
  • 86
  • 126
0
decimal num = 93456;

StringBuilder sb = new StringBuilder(num.ToString());

if (sb.Length > 2)
    sb.Insert(2, Thread.CurrentThread.CurrentCulture
                .NumberFormat.CurrencyDecimalSeparator);

Console.WriteLine(sb);

num = decimal.Parse(sb.ToString());

Console.WriteLine(num);

Update we don't have alot of string manipulation,so don't need using StringBuilder. also according the question we should use NumberDecimalSeparator instead of CurrencyDecimalSeparator

decimal num = 93456;

string str = num.ToString();

if (str .Length > 2)
    str .Insert(2, Thread.CurrentThread.CurrentCulture
                .NumberFormat.NumberDecimalSeparator);

Console.WriteLine(str);

num = decimal.Parse(str);

Console.WriteLine(num);
Mehrdad
  • 1,523
  • 9
  • 23
  • Why the StringBuilder? Also, why `CurrencyDecimalSeparator` and not `NumberDecimalSeparator`? – tymtam Oct 29 '18 at 07:59
  • StringBuilder performance is better because string type is immutable,also we don't have alot of manipulation in string and not matter using string or StringBuilder.but using StringBuilder is a good habit.about NumberDecimalSeparator, you are right,it's better – Mehrdad Oct 29 '18 at 09:44
0

You can try this

string num = TextBox1.Text; //"93456.12";
if (num.Length > 2)
{
    decimal decNumber = Convert.ToDecimal(num.Substring(0, 2) + "." + num.Substring(2, num.Length - 2).Replace(".",""));
     string strNumber = Convert.ToString(decNumber);
}
Suraj Kumar
  • 5,547
  • 8
  • 20
  • 42
0

This may help you:

string num1 = "93456";
int value = Convert.ToInt32(num1);
int digits = (num1.Length - 2) > 0 ? (num1.Length - 2) : 0;
decimal number1 = value / Convert.ToDecimal(Math.Pow(10, digits));
string result = number1.ToString();
ksdev
  • 56
  • 7
0
string num = "9312";
string newNum = num.Length > 2 ? num.Insert(2, ".") : num + ".000";
decimal newDecimal; 

if (Decimal.TryParse(newNum, out newDecimal))
    Console.WriteLine(newDecimal);
else
    Console.WriteLine("not a valid decimal '{0}'.", newDecimal);      
Gauravsa
  • 6,330
  • 2
  • 21
  • 30
0

Yet another way:

string num = "93456";

decimal decinum = decimal.Parse(num);

string newnum = decinum.ToString("#,#", new NumberFormatInfo { NumberGroupSeparator = "." });
Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49