Lucene.Net  3.0.3
Lucene.Net is a .NET port of the Java Lucene Indexing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties
Single.cs
Go to the documentation of this file.
1 /*
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements. See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership. The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License. You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied. See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20 */
21 
22 using System;
23 using System.Globalization;
24 
25 namespace Lucene.Net.Support
26 {
30  public class Single
31  {
39  public static System.Single Parse(System.String s, System.Globalization.NumberStyles style, System.IFormatProvider provider)
40  {
41  if (s.EndsWith("f") || s.EndsWith("F"))
42  return System.Single.Parse(s.Substring(0, s.Length - 1), style, provider);
43  else
44  return System.Single.Parse(s, style, provider);
45  }
46 
53  public static System.Single Parse(System.String s, System.IFormatProvider provider)
54  {
55  if (s.EndsWith("f") || s.EndsWith("F"))
56  return System.Single.Parse(s.Substring(0, s.Length - 1), provider);
57  else
58  return System.Single.Parse(s, provider);
59  }
60 
67  public static System.Single Parse(System.String s, System.Globalization.NumberStyles style)
68  {
69  if (s.EndsWith("f") || s.EndsWith("F"))
70  return System.Single.Parse(s.Substring(0, s.Length - 1), style);
71  else
72  return System.Single.Parse(s, style);
73  }
74 
80  public static System.Single Parse(System.String s)
81  {
82  if (s.EndsWith("f") || s.EndsWith("F"))
83  return System.Single.Parse(s.Substring(0, s.Length - 1).Replace(".", CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator));
84  else
85  return System.Single.Parse(s.Replace(".", CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator));
86  }
87 
88  public static bool TryParse(System.String s, out float f)
89  {
90  bool ok = false;
91 
92  if (s.EndsWith("f") || s.EndsWith("F"))
93  ok = System.Single.TryParse(s.Substring(0, s.Length - 1).Replace(".", CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator), out f);
94  else
95  ok = System.Single.TryParse(s.Replace(".", CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator), out f);
96 
97  return ok;
98  }
99 
105  public static string ToString(float f)
106  {
107  return f.ToString().Replace(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator, ".");
108  }
109 
116  public static string ToString(float f, string format)
117  {
118  return f.ToString(format).Replace(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator, ".");
119  }
120 
121  public static int FloatToIntBits(float value)
122  {
123  return BitConverter.ToInt32(BitConverter.GetBytes(value), 0);
124  }
125 
126  public static float IntBitsToFloat(int value)
127  {
128  return BitConverter.ToSingle(BitConverter.GetBytes(value), 0);
129  }
130  }
131 }