0

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

4 Answers4

3

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;
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

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.

Community
  • 1
  • 1
Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
  • i think this doesn't fix the zero on the left problem – Abdelrahman Elshafiey Mar 18 '13 at 19:00
  • and xbee can send byte by byte so it can send only characters – Abdelrahman Elshafiey Mar 18 '13 at 19:01
  • 1
    yes 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
1

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)
Black Frog
  • 11,595
  • 1
  • 35
  • 66
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";
}
Reem
  • 43
  • 7