numberFun


open System
open System.Text
open FSharp.Collections
open System.Numerics

let srcNumberToTranslate=try Environment.GetCommandLineArgs()[2] with |_ ->"0"
let srcNumberBase=try Environment.GetCommandLineArgs()[3] with |_ -> "10"
let targetNumberBase=try Environment.GetCommandLineArgs()[4] with |_ -> "10"

let isScientificNotation numberString = System.Text.RegularExpressions.Regex.Match(numberString, "[e|E][\+\-]").Length=2
let splitByScientificNotation numberString = System.Text.RegularExpressions.Regex.Split(numberString, "[e|E][\+\-]")
let mainNumber = try (splitByScientificNotation srcNumberToTranslate)[0] with |_ -> "NOOO"
let exponentNumber = try (splitByScientificNotation srcNumberToTranslate)[1] with |_ -> "NOOO"
let exponentSign = if srcNumberToTranslate.Contains("-") then "-" else "+"
let makeSureStringHasDecimalOrAddAtEnd (myString:string) = 
  match System.Text.RegularExpressions.Regex.Split(myString, "\.").Length
    with 
      | 1->(myString + ".")
      | 2->myString
      | _->"0"
let numberWithDecimal=makeSureStringHasDecimalOrAddAtEnd mainNumber

type PaddingNeeded = AddLeadingZeros | MoveDecimalAroundInNumber  | AddTrailingZeros

let moveDecimal (originalString:string) moveDirection expNum =
  let moveCount=try int(expNum) with |_ ->0
  let numSplit = try originalString.Split([|'.'|]) with |_ ->[|"0";"0"|]
  let decimalLocation = try originalString.IndexOf(".") with |_-> originalString.Length
  let moveRight = if moveDirection = "+" then true else false
  let newDecimal = if moveRight then decimalLocation + moveCount else decimalLocation - moveCount
  let whatToDo =
    match newDecimal
      with
        |_ when newDecimal <0 ->(AddLeadingZeros, -1*newDecimal)
        |_ when newDecimal >numSplit[1].Length+2 -> (AddTrailingZeros, 1+newDecimal-originalString.Length)
        |_ -> (MoveDecimalAroundInNumber,newDecimal)

  let explodedNumber =
    match whatToDo
      with
        | (AddLeadingZeros,n)->"." + String('0',snd whatToDo) + numSplit[0] + numSplit[1]
        | (AddTrailingZeros,n)->numSplit[0] + numSplit[1] + String('0',snd whatToDo)
        | (MoveDecimalAroundInNumber,n)->""
  explodedNumber

let superHugeNumberString = moveDecimal (makeSureStringHasDecimalOrAddAtEnd mainNumber) exponentSign exponentNumber

let exponentWalkingDirection = if superHugeNumberString.[0]='.' then -1 else 1
let inputBase = BigInteger.Parse(srcNumberBase)
let temporarySums =
  superHugeNumberString |> Seq.mapi(fun i x-> 
    let exponentMultiplier:bigint = 
      if exponentWalkingDirection = 1 
        then bigint (  superHugeNumberString.Length-1 - i * exponentWalkingDirection)
        else bigint ((i+1)*(-1))
 
 let digitValueMultiplier:bigint = BigInteger.Pow(inputBase,(int)exponentMultiplier)
    let digitValue = BigInteger.Parse(string x)
    let runningResult = digitValueMultiplier * digitValue
    runningResult
    )

let convertedToBaseTen:bigint = temporarySums |> Seq.sum