 ## 2 replies

Last post Jul 10, 2018 11:13 AM by msdevm

• ### How to add to a numeric data type with 1 digit decimal a 2 decimal with leading zero.

How to convert a numeric data type with 1 digit decimal to 2 decimals with leading zero.

For Instance
Assuming that I got a result from a calculation 7.7 which means 7.07, how to add the leading zero or how to convert to a 2 decimal points with leading zero

The Math.Round() rounds it to 7.70 and or 8.0 and what I am looking for to be 2 decimal points with the leading zero, in other word the result should be 7.07

I will really appriciates for sample code that converts from 7.7 to 7.07

Thanks

• ### Re: How to add to a numeric data type with 1 digit decimal a 2 decimal with leading zero.

Jul 10, 2018 03:15 AM|Brando ZWZ|LINK

Hi msdevm,

#### msdevm

The Math.Round() rounds it to 7.70 and or 8.0 and what I am looking for to be 2 decimal points with the leading zero, in other word the result should be 7.07

I will really appriciates for sample code that converts from 7.7 to 7.07

In my opinion. it is more reasonable to get the result 7.70 instead of 7.07 because 7.70 equals to 7.7.

If you have special requirement convering the 7.7 to 7.07, I suggest you could conver the number to string and use split() method to split the number and add a “0” character to the split number.

More details, you could refer to below codes:

```protected void Page_Load(object sender, EventArgs e)
{

double d = 7.7;
Label1.Text = MyConverter(d).ToString();
}

public double MyConverter(double d)
{
string str=  d.ToString();
string[] strArr = str.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
string result = strArr + "." + "0" + strArr;
return double.Parse(result);
}

```

Result: Best Regards,

Brando

MSDN Community Support