Please Help ----- Getting wrong result in C# than VB.Net --- Urgent

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pargat.singh@gmail.com

    Please Help ----- Getting wrong result in C# than VB.Net --- Urgent

    Hi Everyone:

    Please help as i need to write some rounding function in C#. i
    Wrote one but does not give me correct result. Can some one please
    correct me

    C# Codes

    ############### ############### ############### ############### #############
    dblVal = 1234567.89

    private int ThreeSifFig(dou ble dblVal)
    {
    int lngvalue;
    int threesigfig;
    int lngvaluelen;

    lngvalue = (int)(Math.Abs( dblVal));
    lngvaluelen = lngvalue.ToStri ng().Length;
    if ((lngvaluelen <= 1))
    {
    threesigfig = 0;
    }
    else if ((lngvaluelen <= 3))
    {
    threesigfig = (lngvalue / 10) * 10;
    }
    else
    {
    threesigfig = (lngvalue / 10 ^ (lngvaluelen - 3) * 10) ^
    (lngvaluelen - 3);
    }
    if ((dblVal < 0))
    {
    threesigfig = (threesigfig * -1);
    }

    return threesigfig;

    }

    Return Result is 123500 but the expected result is 1230000

    ############### ############### ############### ############### ###############

    VB.NET code which return correct result
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArg s) Handles Me.Load

    Dim sval As Double = 1234567.89
    Dim lngvalue, lngvaluelen As Long
    Dim threesigfig As Long
    lngvalue = Int(Math.Abs(Va l(sval)))
    lngvaluelen = Len(CStr(lngval ue))
    If lngvaluelen <= 1 Then
    threesigfig = 0
    ElseIf lngvaluelen <= 3 Then
    threesigfig = Int(lngvalue / 10) * 10
    Else
    threesigfig = Int(lngvalue / 10 ^ (lngvaluelen - 3)) * 10
    ^ (lngvaluelen - 3)
    End If
    If sval < 0 Then
    threesigfig = threesigfig * -1
    End If
    End Sub

    Result is 1230000 [Correct]

    Thanks in advance.
  • =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?=

    #2
    RE: Please Help ----- Getting wrong result in C# than VB.Net --- Urgen

    That is a very complicated way to find the value you are after, and,
    it is not generic. If you wanted to find the value to six digits, you would
    need a new function.

    You should find the integer log base 10 value for the number. So for your
    value of 1234567.89, you have a log base 10 equal to 6. Now if you want
    three significant digits, subtract two (or one less than the sig digits) off
    the
    value of six. This gives you four. Raise 10 to the 4th and you have 10000.

    Divide your value by the power of ten just computed (1234567.89 / 10000)
    and get the integer value (123). Now multiply the result by the 10000.

    Result = 1230000.

    Hope this helps.


    "pargat.singh@g mail.com" wrote:
    Hi Everyone:
    >
    Please help as i need to write some rounding function in C#. i
    Wrote one but does not give me correct result. Can some one please
    correct me
    >
    C# Codes
    >
    ############### ############### ############### ############### #############
    dblVal = 1234567.89
    >
    private int ThreeSifFig(dou ble dblVal)
    {
    int lngvalue;
    int threesigfig;
    int lngvaluelen;
    >
    lngvalue = (int)(Math.Abs( dblVal));
    lngvaluelen = lngvalue.ToStri ng().Length;
    if ((lngvaluelen <= 1))
    {
    threesigfig = 0;
    }
    else if ((lngvaluelen <= 3))
    {
    threesigfig = (lngvalue / 10) * 10;
    }
    else
    {
    threesigfig = (lngvalue / 10 ^ (lngvaluelen - 3) * 10) ^
    (lngvaluelen - 3);
    }
    if ((dblVal < 0))
    {
    threesigfig = (threesigfig * -1);
    }
    >
    return threesigfig;
    >
    }
    >
    Return Result is 123500 but the expected result is 1230000
    >
    ############### ############### ############### ############### ###############
    >
    VB.NET code which return correct result
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArg s) Handles Me.Load
    >
    Dim sval As Double = 1234567.89
    Dim lngvalue, lngvaluelen As Long
    Dim threesigfig As Long
    lngvalue = Int(Math.Abs(Va l(sval)))
    lngvaluelen = Len(CStr(lngval ue))
    If lngvaluelen <= 1 Then
    threesigfig = 0
    ElseIf lngvaluelen <= 3 Then
    threesigfig = Int(lngvalue / 10) * 10
    Else
    threesigfig = Int(lngvalue / 10 ^ (lngvaluelen - 3)) * 10
    ^ (lngvaluelen - 3)
    End If
    If sval < 0 Then
    threesigfig = threesigfig * -1
    End If
    End Sub
    >
    Result is 1230000 [Correct]
    >
    Thanks in advance.
    >

    Comment

    • =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?=

      #3
      RE: Please Help ----- Getting wrong result in C# than VB.Net --- Urgen

      threesigfig = (lngvalue / 10 ^ (lngvaluelen - 3) * 10) ^
      (lngvaluelen - 3);
      >
      The above line does not represent a translation from the VB code. Look, for
      example, at the result of 10 ^ 4 versus what you think it should be.

      Comment

      Working...