libs-base/Source/NSEnergyFormatter.m

130 lines
3.3 KiB
Mathematica
Raw Permalink Normal View History

/** Implementation of class NSEnergyFormatter
2019-10-09 11:32:30 +00:00
Copyright (C) 2019 Free Software Foundation, Inc.
2019-10-22 19:00:43 +00:00
By: Gregory John Casamento <greg.casamento@gmail.com>
2019-10-09 11:32:30 +00:00
Date: Tue Oct 8 13:30:10 EDT 2019
This file is part of the GNUstep Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
2019-10-09 11:32:30 +00:00
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 31 Milk Street #960789 Boston, MA 02196 USA.
2019-10-09 11:32:30 +00:00
*/
#import "Foundation/NSEnergyFormatter.h"
#import "Foundation/NSMeasurement.h"
#import "Foundation/NSMeasurementFormatter.h"
#import "Foundation/NSNumberFormatter.h"
#import "Foundation/NSUnit.h"
2019-10-09 11:32:30 +00:00
@implementation NSEnergyFormatter
2019-10-24 04:03:40 +00:00
- (instancetype) init
{
self = [super init];
if (self != nil)
2019-10-24 04:03:40 +00:00
{
_numberFormatter = nil;
_unitStyle = NSFormattingUnitStyleMedium;
_isForFoodEnergyUse = NO;
}
return self;
}
2019-10-22 19:00:43 +00:00
- (NSNumberFormatter *) numberFormatter
{
return _numberFormatter;
}
- (void) setNumberFormatter: (NSNumberFormatter *)formatter
{
ASSIGN(_numberFormatter, formatter);
}
- (NSFormattingUnitStyle) unitStyle
{
return _unitStyle;
}
- (void) setUnitStyle: (NSFormattingUnitStyle)style
{
_unitStyle = style;
}
- (BOOL) isForFoodEnergyUse
{
return _isForFoodEnergyUse;
}
- (void) setForFoodEnergyUse: (BOOL)flag
{
_isForFoodEnergyUse = flag;
}
2019-10-22 19:00:43 +00:00
- (NSString *) stringFromValue: (double)value unit: (NSEnergyFormatterUnit)unit
{
2019-10-24 21:06:06 +00:00
NSUnit *u = nil;
NSMeasurement *m = nil;
NSMeasurementFormatter *mf = nil;
switch (unit)
2019-10-24 21:06:06 +00:00
{
case NSEnergyFormatterUnitJoule:
u = [NSUnitEnergy joules];
break;
case NSEnergyFormatterUnitKilojoule:
u = [NSUnitEnergy kilojoules];
break;
case NSEnergyFormatterUnitCalorie:
u = [NSUnitEnergy calories];
break;
case NSEnergyFormatterUnitKilocalorie:
u = [NSUnitEnergy kilocalories];
break;
}
m = [[NSMeasurement alloc] initWithDoubleValue: value
unit: u];
AUTORELEASE(m);
mf = [[NSMeasurementFormatter alloc] init];
AUTORELEASE(mf);
[mf setUnitStyle: _unitStyle];
[mf setNumberFormatter: _numberFormatter];
return [mf stringFromMeasurement: m];
2019-10-22 19:00:43 +00:00
}
- (NSString *) stringFromJoules: (double)numberInJoules
{
2019-10-24 21:06:06 +00:00
return [self stringFromValue: numberInJoules unit: NSEnergyFormatterUnitJoule];
2019-10-22 19:00:43 +00:00
}
- (NSString *) unitStringFromValue: (double)value unit: (NSEnergyFormatterUnit)unit
{
2019-10-24 21:06:06 +00:00
return [self stringFromValue: value unit: unit];
2019-10-22 19:00:43 +00:00
}
2019-10-24 21:06:06 +00:00
- (NSString *) unitStringFromJoules: (double)numberInJoules usedUnit: (NSEnergyFormatterUnit *)unit
2019-10-22 19:00:43 +00:00
{
2019-10-24 21:06:06 +00:00
*unit = NSEnergyFormatterUnitJoule;
return [self stringFromValue: numberInJoules unit: *unit];
2019-10-22 19:00:43 +00:00
}
- (BOOL) getObjectValue: (id *)obj forString: (NSString *)string errorDescription: (NSString **)error
2019-10-22 19:00:43 +00:00
{
return NO;
}
2019-10-09 11:32:30 +00:00
@end