How to convert 30.55273 to 30.055273 in C# i am working with xbee wireless module and it doesn't send fraction numbers so i have to cut any double value into two parts EX: 30.055273 -> 30 and 055273 so when i send both of them i receive them 30 and 55273 so the zero on the left will be canceled how can i fix this problem
-
@Ab...are you cutting them into two strings? – MikeTWebb Mar 18 '13 at 18:44
4 Answers
It sounds like you're receiving two integers, which you want to stick together - with one of them scaled by a factor of a million. Is that right?
double ConvertToDouble(int wholePart, int fractionalPart)
{
return wholePart + fractionalPart / 1000000d;
}
Note that the "d" part is very important - it's to make sure you perform the division using double
arithmetic (instead of integer arithmetic).
If the exact digits are important to you, it's possible that you should actually be using decimal
:
decimal ConvertToDouble(int wholePart, int fractionalPart)
{
return wholePart + fractionalPart / 1000000m;
}

- 1,421,763
- 867
- 9,128
- 9,194
-
yes i want to stick them together and the fraction part is always 6 digits – Abdelrahman Elshafiey Mar 18 '13 at 18:48
-
@Jon....if fractionalPart has more than 5 didigts, will this approach work? I'm just trying to figure how the denominator would work for values of, say, 0552734, 05527346, etc. – MikeTWebb Mar 18 '13 at 18:49
-
@Jon....disregard my comment, I just saw Abdelrahman's comment above regarding the 6 digits :) – MikeTWebb Mar 18 '13 at 18:50
just choose a multiplier, like 100000, take the fractional part and multiply it by that number, then later divide by that number.
Also, you can send whatever data you like over XBee. You may just want to convert your data into an array of bytes. See How do I convert an array of floats to a byte[] and back? on how to do that.

- 1
- 1

- 43,549
- 15
- 93
- 156
-
-
and xbee can send byte by byte so it can send only characters – Abdelrahman Elshafiey Mar 18 '13 at 19:01
-
1yes it, fixes it, it just sends the data in byte form..... bytes are not quite the same as characters. characters are just one particular encoding of bytes to represent certain symbols. XBee can send bytes just like the ram in your computer stores everything as bytes. floats are just a way of encoding 4 bytes to represent a floating point number, if you send those 4 bytes, you will get a floating point on the other end. I use XBee and send floating point data as well as other data ( also compressed and encryped on top of that! ) – Keith Nicholas Mar 18 '13 at 20:29
-
@ Keith Nicholas : you seem to be professional at xbee so could you help me in the compression and encryption part may be with some useful links or tutorials , thanks in advance – Abdelrahman Elshafiey Mar 19 '13 at 13:45
Can you send them as a string? Then do that.
If you can only send integer values, then send 3 values.
For example: 30.055273
- First number is left of decimal (whole number)
- Second number is right of decimal (fraction)
- Third number is the number of zeros (placeholder)

- 11,595
- 1
- 35
- 66
-
so when i know how much zeros in the fraction part how can i construct them back in C# – Abdelrahman Elshafiey Mar 18 '13 at 18:59
-
1
Building on Bloack Frog's idea. Construct a string in c# Then cast it to double as follows:
String result = leftToDecimal.ToString()
+ "." + zeroString(numberOfZeros) + fraction.ToString();
double MyOriginalNumber = Convert.ToDouble(result);
Public string zeroString(int zeroCount)
{
String result= "";
for(int i=0; i< zeroCount; i++)
result+="0";
}

- 43
- 7
-
-
i have solved the problem , but i really appreciate your help ya reem :D – Abdelrahman Elshafiey Mar 18 '13 at 19:24
-
1:) i was abt to tell u that u can use delimiters w kda bs seems like u've found ur way. Thnx – Reem Mar 18 '13 at 19:30