开发者

convert Javascript function to c#

开发者 https://www.devze.com 2022-12-20 09:32 出处:网络
I want to convert following javascript function to c# can anyone help? function parseCoordinate(coordinate,type,format,spaced) {

I want to convert following javascript function to c# can anyone help?

function parseCoordinate(coordinate,type,format,spaced) {
    coordinate = coordinate.toString();
    coordinate = coordinate.replace(/(^\s+|\s+$)/g,''); // remove white space
    var neg = 0; if (coordinate.match(/(^-|[WS])/i)) { neg = 1; }
    if (coordinate.match(/[EW]/i) && !type) { type = 'lon'; }
    if (coordinate.match(/[NS]/i) && !type) { type = 'lat'; }
    coordinate = coordinate.replace(/[NESW\-]/gi,' ');
    if (!coordinate.match(/[0-9]/i)) {
        return '';
    }
    parts = coordinate.match(/([0-9\.\-]+)[^0-9\.]*([0-9\.]+)?[^0-9\.]*([0-9\.]+)?/);
    if (!parts || parts[1] == null) {
        return '';
    } else {
        n = parseFloat(parts[1]);
        if (parts[2]) { n = n + parseFloat(parts[2])/60; }
        if (parts[3]) { n = n + parseFloat(parts[3])/3600; }
        if (neg && n >= 0) { n = 0 - n; }
        if (format == 'dmm') {
            if (开发者_开发技巧spaced) {
                n = Degrees_to_DMM(n,type,' ');
            } else {
                n = Degrees_to_DMM(n,type);
            }
        } else if (format == 'dms') {
            if (spaced) {
                n = Degrees_to_DMS(n,type,' ');
            } else {
                n = Degrees_to_DMS(n,type,'');
            }
        } else {
            n = Math.round(10000000 * n) / 10000000;
            if (n == Math.floor(n)) { n = n + '.0'; }
        }
        return comma2point(n);
    }
}


Check out Regex on MSDN or have a quick look on google for Regex C#

if (coordinate.match(/(^-|[WS])/i)) { neg = 1; } 

would become:

using System.Text.RegularExpressions;

Regex myRegex = new Regex("/(^-|[WS])/i)");
if (coordinate.IsMatch(myRegex))
{
  neg=1;
}

If it will always be like your above example 'N27 53.4891' then you could store it as a string. If the above latitude is in 2 parts ('N27' and 53.4891) and you need to access them seperately then you could have a custom Coordinate class, e.g.

public class coordinate
{
  public string otherPart {get; set;} // N27
  public float coordPart {get; set;} // 53.4891
}

You can then override the .toString() method to get 'N27 53.4891'.


Regex myPattern =new Regex("/(^-|[WS])/i)");
if(myPattern.isMatch(coordinate))
    { neg = 1; }

see if this works

0

精彩评论

暂无评论...
验证码 换一张
取 消