Click here to Skip to main content
15,917,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello to all,
I have a numbers in the format like:

15.2564
4.5268
235.9985
0.5656
78.3211

What I would like to get is:

0152564
0045268
2359985
0005656
0783211

What is the best way to remove the decimal point from double values and to add:
1.no zeros in front of the value if the value has three numbers before decimal point (example 235.9985 will be 2359985)
2.one zero in front of the value if the value has two numbers before decimal point (example 15.2564 will be 0152564)
3. two zeros in front of the value if the value has one number before decimal point (example 4.5268 will be 0045268)

What I have tried:

In the meaning time I am searching for the similar example.
I have found a solution for the second part related to format.
VB
Dim dblValue As Double = 15.2564
Dim fmt As String = "000.####"
Console.WriteLine(dblValue.ToString(fmt))
Posted
Updated 5-Jul-17 23:52pm
v2
Comments
F-ES Sitecore 6-Jul-17 5:22am    
Smells like homework, but you're better converting your numbers to strings and doing this using string manipulation, it'll be much easier than doing it numerically. Look at functions like IndexOf and Replace, they'll get you want you need.

Try this
decimal decNum = 0.5656M;
var strArr = decNum.ToString().Split('.').ToArray();
string finalString = strArr[0].PadLeft(3, '0') + strArr[1];
 
Share this answer
 
Comments
blueye89 6-Jul-17 6:39am    
Converted to VB code:
Dim decNum As Decimal = 0.5656D
Dim strArr = decNum.ToString().Split("."C).ToArray()
Dim finalString As String = strArr(0).PadLeft(3, "0"C) + strArr(1)

Thank you very much!
Richard Deeming 6-Jul-17 9:03am    
The Split function already returns an array, so there's no need to call ToArray on the returned value. :)

You'll also want to check the Length of the array, in case the number is an integer.
blueye89 7-Jul-17 2:34am    
Yes, I would like also to check the length of the array if number is integer. For example if the fixed length is 6 digits and I have got array which length is 5, then I would like to add one "0" at the end of the string to fill the length of 6 digits. Any idea?
try to multiply by 10000 to remove the decimal point.
 
Share this answer
 

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