Convert from British coordinates to standard WGS84 nmea - c#

I was posting similar post already. And I did get an answer in theory but I really need somone to help me.
So I am using this EXAMPLE for testing my GPS. What I want to do is how to get standard decimal values like 56,322415 for latitude and longitude.
Because now I am getting 5304,254 value.
I need it in this form because I will use this for Google maps.
Can you check code and tell me what should I left out or how to convert it to WGS84 format.
Thank you.
EDIT:
Here is the picture that is explaining what I see when I run the program
And this is the one that is one the codeproject page:
EDIT 2:
So when I use THIS CONVERTER to see what decimal values I need to get when I have degrees I don't get the same values.
This is the value that I should get :
And this is the value that I get in the program:
46.64662666666667
Any idea why I can't get same conversion?

the following line in the ParseNMEA function is the one that does the conversion from WGS84 to the British Ordinance Survey coordinates:
return Transform(deciLat, deciLon, height);
so if you remove that line and just use deciLat and deciLon values, you should be fine...
Addition after question update:
The code you're referring to does not take local number formats into account, so if this goes wrong, it might be because of erroneous decimal symbol matching.
You can use the following functions to do the conversion, presuming the input is always in the invariant culture (with decimal points):
string[] arrLon = strLon.Split(new char[]{'°', '"'}, StringSplitOptions.RemoveEmptyEntries);
double dblLon = double.Parse(arrLon[0]) + double.Parse(arrLon[1], new System.Globalization.NumberFormatInfo()) / 60;
double deciLon = arrLon[2] == "E" ? dblLon : - dblLon;
Additional info:
NumberFormatInfo: http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.aspx

It sounds like your problems are now solved, I do know that there were issues caused by different local number formatting which were raised and resolved in one of the comments:
http://www.codeproject.com/Articles/13577/GPS-Deriving-British-Ordnance-Survey-Grid-Referenc?msg=2106791#xx2106791xx
If you simply want decimal values for lat and lon, I guess you can throw the entire thing away now and just use the solution provided by Dirk! =o)
Alex

Related

Spliting a number into equal parts and Add it together to get the same number

I need to implement a logic in c# where as I need to split and Add it to together to get the same value.
For example:
1.0/6.0442137639369475 = 0.16544749061764519
and when I add
0.16544749061764519 + 0.16544749061764519
to make it 1.0 is not working at all it comes as
0.99268494370587
not 1.0 exactly.
I am not sure what I am missing...?
Try to use more precise dataformats. Try to use double. If this is not precise enough, what I doubt, you can still search for a more precise implementation.
if I guess your question correct, you want to do the following math:
c=a/b
d=c+c+c+... n-times until a==d.
this will only work if you use integer numbers for a and b, with your real number 6.0442137639369475 as b it cannot (math law afaik).
And by the way:
your division result 0.16544749061764519 is rounded! The correct result is 0.16544749061764518326.
So you maybe need another data type as already pointed out.

Split Double Values?

i have two values written like this " +5.000" (first space then plus and the double value, double value is height in meters)
the first one is on textBox and second one is i'm receiving via ref key.
simply enough i want to get the result firstvalue - secondvalue = result
for example ( +5.000 - +2.800 = 2.200)
result only in digits without plus.
i have asked this question also on c# forum
https://social.msdn.microsoft.com/Forums/en-US/a024e097-8013-4771-bbf6-99c7fd4cf457/double-split-
Thanks and Best Regards
OK, I think you're trying to solve a few problems here in one question. Let's break it down.
You need to get a value from a text box. I'll assume its called txtBox in the absense of any code, so you need to write:
double a = Double.parse(txtBox.Text);
You then need to perform your calculation. This needs to be written the other way around, for example:
result = a - b;
With limited source code it's difficult to answer properly.

Parsing in CSharp - how to understand it

It's just few days ago that I jumped into learning C# and I already have one problem with understanding basics.. Maybe it's just the language barrier (I'm not English native speaker). Please, could you explain me how to understand parsing? For example: while creating a very simple calculator I wanted to read the first input number (which is a variable a). I use this code:
float a = float.Parse(Console.ReadLine());
and the same with b for the other number:
float b = float.Parse(Console.ReadLine());
I learnt that the float is a data type for decimals numbers so what exactly does this particular Parse() stands for?
Obviously, I tried to run the application without parsing and it wouldn't work because it reads it as string, but why? Thank you..
Console.ReadLine() returns a string, which represents a piece of text. So, from the computer's point of view, what you have after calling Console.ReadLine() is a piece of text. It may or may not contain the text "6.0", but from the computer's point of view, it is just a piece of text. As such, you cannot use it to add, subtract etc.
Using the float.Parse(...) method, you tell the computer: "This piece of text actually represents a floating point number, could you please read the text and give me back a number, so that I can start doing math with it?".
The method you are using, float.Parse() is just one of many such methods that take a String input value, and attempt to convert it into the target type, here a float.
There is a safer alternative, however, and it is TryParse():
float a;
if (float.TryParse(Console.ReadLine(), out a))
{
//do something with your new float 'a'
}
In either case, your are asking the framework to inspect the value you provide, and attempt to make a conversion into the requested type. This topic can be quite deep, so you'll want to consult MSDN for the specifics.
Console.ReadLine reads text that the user inputs and returns it to the program so that you may do with it what you want. Therefore, the ReadLine method returns a string.
If you want to work with a decimal (check the decimal class instead of float), you need to convert the string, which is a character sequence, to a number of your desired type, that's where float.Parse comes in:
float.Parse accepts a string and if possible, returns a float value.
Almost every type contains the Parse method which is used to transform a string into the calling one.

How to format currency from database

The goal
I want to format correctly the currency from database.
The problem
I'm formatting the currency from database with this:
#String.Format("{0:C}", #Model["MinProductPrice"])
The problem is: 150 have to be 1,50, and not 150,00 — and this formatting is doing this.
What is the right formatting type to my case?
You probably want to divide the number by 100 first (remember to change the type), so 150 becomes 1.50, which gets converted to "1,50" depending on locale:
#String.Format("{0:C}", #Model["MinProductPrice"] / 100.0m)
I'll extend my comments into an answer, I think that's more appropriate. I think you should change the column type to a money or decimal type to prevent bugs by making the use of the column more obvious. Your output on your page will be correct and won't require any "magic numbers" to get it to print out properly.
Just a note but you can also print a currency string doing this:
#Model["MinProductPrice"].ToString("C")
I attributed the casting responsibility to database. I'm using MySQL and the query is like this:
ROUND(CAST(MIN(`map`.`Product_Price`) AS DECIMAL)/100,2) as `minProductPrice`
Anyway, I would to thanks jlafay and csharpler about their answers — they were very helpful and worked well for me.

sql type float, real, decimal?

well in my database i had a colum for price of one product
i had it as float, my problem is if i saved it since my c# application
as 10.50 .. in a query it returns 10,50 and if i update i get a error
10,50 cant convert to float ... or something so..
and if i saved it as decimal, in queries inside sql management .. are ok..
but in my c# application... i get the same error..
10.50 retuns as 10,50 i dont know why, and how to solved it.. my unique solution is saved it
as varchar...
That's a localisation problem of some sort. 10,50 is the "European" way of writing ten and a half. If you're getting that from your select statements then your database is probably configured incorrectly.
Generally speaking you should use the same type throughout your layers. So if the underlying types in the database are x, you should pass around those data with identical types in c#, too.
What type you choose depends on what you are storing--you shouldn't be switching around types just to get something to "work". To that end, storing numeric data in a non-numeric type (e.g. varchar) will come back to bite you very soon. It's good you've opened this question to fix that!
As others have miraculously inferred, you are likely running into a localization issue. This is a great example of why storing numbers as strings is a problem. If you properly accept user input in whatever culture/localization they want (or you want), and get it into a numeric-type variable, then the rest (talking to the DB) should be easy. More so, you should not do number formatting in the database if you can help it--that stuff is much better placed at the front end, closer to the users.
I think your setting in windows regional and language for decimal symbol is wrong.please set it to dot and again test it.
This may help out for temporary use but I wouldn't recommend it for permanent use:
Try making it so that just before you save the file, convert the number to a string, replace the commas with periods (From , to .) and then save it into the database as the string, hopefully it should see that it is in the correct format and turn it into what the database sees as "Decimal" or "Floating".
Hope this helps.
Yep, localization.
That said, I think your pice is being stored on a "money" field in SQLServer (I'm assuming it's SQLServer you're using). If that was a float in the DB, it would return it with a normal decimal point, and not the European money separator ",".
To fix:
Fist DO NO USE FLOAT in your c# code, unless you absolutely require a floating point number. Use the decimal type instead. That's not just in this case, but in all cases. Floating point numbers are binary (base-2), not decimal (base-10), so what you see in the interface is only a decimal approximation of the actual number. The result is that frequently (1 == 1) evaluates as false!
I've run into that problem myself, and it's maddening if you don't know that can happen. Always use decimal instead of float in c#.
Ok, after you've fixed that, then do this to get the right localization:
using System.Globalization;
...
NumberFormatInfo ni = new NumberFormatInfo();
ni.CurrencyDecimalSeparator = ",";
decimal price = decimal.Parse(dbPriceDataField, ni);
Note that "dbPriceDataField" must be a string, so you may have to do a ".ToString()" on that db resultset's field.
If you end up having to handle other "money" aspects of that money field, like currency symbols, check out: http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.aspx
If you need more robust error handling, either put that decimal.Parse in a try/catch, or use decimal.TryParse.
EDIT --
If you know what culture (really, country), the db is set to, you can do this instead:
using System.Globalization;
...
CultureInfo ci = new CultureInfo("fr-FR"); // fr-FR being "french France"
decimal price = decimal.Parse(dbprice, ci.NumberFormat);
Such problems were faced by me in my Web Apps... but i found the solution like I was fetching my price value in textbox. So I was have database attached with that. So when you attached your database with textbox... When you right click textbox and click Edit DataBinding.... in that you have to provide.... type like in Bind Property..... {0:N2}
This will work only for web apps or websites... not for desktop applications...

Categories