Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI Team,
I have tried to covert the complete decimal number into a string for the XML output.
Ex: decimal mynumber = 123.768686785768759574945595 (after decimal point more than 10 digits)
The above complete decimal needs to convert into string and pass to XML output.
Could you please help me on this.

The above same value need to convertinto string and pass to the below xml element
Expected output: <myvalue>123.768686785768759574945595

What I have tried:

using System;
class MyTest{
  static void Main() {
      decimal munumber = 123.768686785768759574945595;
      string strnumber = Convert.ToString(munumber);
    Console.WriteLine(strnumber);
  }
}
Posted
Updated 17-Jun-20 9:39am
v2
Comments
Richard MacCutchan 17-Jun-20 13:00pm    
What is the problem?
DGKumar 17-Jun-20 13:59pm    
HI Richard
I am getting the below error while executing that
error CS0664: Literal of type double cannot be implicitly converted to type `decimal'. Add suffix `m' to create a literal of this type
Richard MacCutchan 17-Jun-20 17:01pm    
I think you need to understand the difference between decimal and floating point numbers.

Try
C#
string strnumber = munumber.ToString();
If you want a specific format for the string representation of the number, then use one of the overloads of the ToString() method:
Decimal.ToString Method (System) | Microsoft Docs[^]
 
Share this answer
 
v2
Comments
DGKumar 17-Jun-20 14:00pm    
HI Phil
I am getting the below error while executing
error CS0664: Literal of type double cannot be implicitly converted to type `decimal'. Add suffix `m' to create a literal of this type
[no name] 17-Jun-20 14:12pm    
Nope sorry, the neutrall 3 is from my side...
DGKumar 17-Jun-20 14:18pm    
Hi
All these are only getting up to 10 digits but remaining digits are not getting.
If I put 'm' at the end of value and convert into string then able to get complete decimal value.

Like below:
decimal munumber = 123.768686785768759574945595m;
string strnumber = Convert.ToString(munumber);

Then same value i am able to get into string to pass further ..
Quote:
Literal of type double cannot be implicitly converted to type `decimal'. Add suffix `m' to create a literal of this type
So did you do exactly what it said?
C#
decimal munumber = 123.768686785768759574945595m;
                                               ^
                              Added the m to the end of the literal
 
Share this answer
 
So read the docs e.g. like here Floating-point numeric types - C# reference | Microsoft Docs[^] and you will see you should do it like this
C#
// please note the 'm' for 123.768686785768759574945595m;
decimal munumber = 123.768686785768759574945595m;
string strnumber = Convert.ToString(munumber);
Console.WriteLine(strnumber);

I hope it helps
 
Share this answer
 
v2
Comments
DGKumar 17-Jun-20 14:20pm    
HiAll these are only getting up to 10 digits but remaining digits are not getting.
If I put 'm' at the end of value and convert into string then able to get complete decimal value.
But the value coming from other source with out 'm' in that case how to add 'm' at the end of decimal value ?
[no name] 17-Jun-20 14:22pm    
I see all the digits in my test... console shows: 123.768686785768759574945595
[no name] 17-Jun-20 14:31pm    
You: "But the value coming from other source with out 'm' in that case how to add 'm' at the end of decimal value ?"

So your problem is more how to convert a string to decimal?

[Edit]
Also this works for me and shows the whole decimal numbere.g.

string munumberInput = "123.768686785768759574945595";
decimal munumber = Convert.ToDecimal(munumberInput);
string strnumber = Convert.ToString(munumber);
Console.WriteLine(strnumber);
DGKumar 17-Jun-20 14:44pm    
From source the value is coming like this: 123.768686785768759574945595
then am trying to convert into string but getting only 10 digits after (.)dot
[no name] 17-Jun-20 14:45pm    
Also this works for me and shows the whole decimal numbere.g.

string munumberInput = "123.768686785768759574945595";
decimal munumber = Convert.ToDecimal(munumberInput);
string strnumber = Convert.ToString(munumber);
Console.WriteLine(strnumber);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900