two decimal for label

RAVI 1,056 Reputation points
2024-05-04T14:42:56.72+00:00

Hello

This is my sample code

Label LRO = e.Item.FindControl("LRO") as Label;

        LRO.Text  = roff; 

         

its should return value in something example two digit decimal

12.45

14.00

15.12

how to do so thanks

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,396 questions
ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
314 questions
{count} votes

3 answers

Sort by: Most helpful
  1. AgaveJoe 27,421 Reputation points
    2024-05-04T19:06:57.44+00:00

    its should return value in something example two digit decimal

    You have asked this formatting question many times recently. Read the docs...

    https://video2.skills-academy.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings

    https://video2.skills-academy.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings

    The following example assumes LRO exists and has a value.

    Label LRO = e.Item.FindControl("LRO") as Label;
    string val = LRO.Text
    decimal val = 0m;
    if(!decimal.TryParse(stringVal, out val))
    {
        //There was an error parsing the string
    }
    Console.WriteLine(val.ToString("#.##"));
    
    0 comments No comments

  2. Lan Huang-MSFT 28,821 Reputation points Microsoft Vendor
    2024-05-06T07:49:03.0633333+00:00

    Hi @RAVI,

    You can use the following code:

    string roff = "12";
    Label LRO = e.Item.FindControl("LRO") as Label;
    decimal val = Convert.ToDecimal(roff);
    LRO.Text = val.ToString("n2");  
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. Sushama Agrawal 1 Reputation point
    2024-05-27T04:55:50.2333333+00:00

    You can use following code:

    Label LRO = e.Item.FindControl("LRO") as Label; 
    if (LRO != null) 
    { 
    	// Assuming 'roff' is a numeric value
    	double roffValue; 
    	if (double.TryParse(roff, out roffValue)) 
    	{ 
    		LRO.Text = roffValue.ToString("F2"); 
    	} 
    	else 
    	{ 
    		LRO.Text = "Invalid value"; 
    	} 
    }
    
    0 comments No comments